在托管 C++ 中创建 KeyValuePair

发布于 2024-07-10 09:04:54 字数 674 浏览 5 评论 0原文

我还有另一个托管 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 的键和值?

I have yet another managed C++ KeyValuePair question where I know what to do in C#, but am having a hard time translating to managed C++. Here is the code that does what I want to do in C#:

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

I've reflected it into MC++ and get this:

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

which I'm translating to:

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

I know from my previous question that KeyValuePair is a value type; is the problem that it's a value type in C++ and a reference type in C#? Can anyone tell me how to set the key and value of a KeyValuePair from C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心安伴我暖 2024-07-17 09:04:54

这应该可以做到:

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

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

This should do it:

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

KeyValuePair is an immutable type, so you have to pass everything to the constructor, which looks the same as in C#, except you write it like this if the object is on the stack.

无敌元气妹 2024-07-17 09:04:54

尝试

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"))   ;

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"))   ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文