Domanda

Ho ancora un'altra domanda KeyValuePair C++ gestito in cui so cosa fare in C#, ma ho difficoltà a tradurre in C++ gestito.Ecco il codice che fa quello che voglio fare in C#:

KeyValuePair<String, String> KVP = new KeyValuePair<string, string>("this", "that");

L'ho riflesso in MC++ e ottengo questo:

KeyValuePair<String __gc*, String __gc*> __gc* KVP = (S"this", S"that");

in cui sto traducendo:

KeyValuePair<String ^, String ^> KVP = (gcnew String("this"), gcnew String("that"));

Lo so dalla mia domanda precedente che KeyValuePair è un tipo di valore;il problema è che si tratta di un tipo di valore in C++ e di un tipo di riferimento in C#?Qualcuno può dirmi come impostare la chiave e il valore di un KeyValuePair da C++?

È stato utile?

Soluzione

Questo dovrebbe farlo:

KeyValuePair< String ^, String ^> k(gcnew String("Foo"), gcnew String("Bar"));

KeyValuePair è un tipo immutabile, quindi devi passare tutto al costruttore, che assomiglia a C#, tranne che lo scrivi in ​​questo modo se l'oggetto è nello stack.

Altri suggerimenti

try

System::Collections::Generic::KeyValuePair< System::String^, System::String^>^ k = gcnew System::Collections::Generic::KeyValuePair< System::String^, System::String^>(gcnew System::String("foo") ,gcnew System::String("bar"))   ;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top