4

我还有另一个托管 C++ KeyValuePair 问题,我知道在 C# 中做什么,但是很难转换为托管 C++。这是我想在 C# 中执行的代码:

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

我已经将它反映到 MC++ 中并得到了这个:

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

我要翻译成:

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

我从上一个问题中知道 KeyValuePair 是一种值类型;问题是它是 C++ 中的值类型和 C# 中的引用类型吗?谁能告诉我如何从 C++ 设置 KeyValuePair 的键和值?

4

2 回答 2

3

这应该这样做:

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

KeyValuePair 是一个不可变类型,因此您必须将所有内容传递给构造函数,这看起来与 C# 中的相同,除了如果对象在堆栈上,您可以这样写。

于 2008-12-05T16:07:20.237 回答
1

尝试

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"))   ;
于 2008-12-05T16:19:21.413 回答