如何将以下C ++代码重写为C#?
std::string someVariable;
std::map<std::string, std::function<void (std::string)>> myMap;
myMap.insert(std::make_pair("someText", [&](std::string input){someVariable = input;});
我曾尝试与代表一起玩,但我还不太了解它。
答案 0 :(得分:1)
不知道你为什么要这样做,但这里是等效的C#代码:
string someVariable = string.Empty;
Dictionary<string, Action<string>> map = new Dictionary<string, Action<string>>();
map.Add("someText", (input) => someVariable = input);
map["someText"]("someInput");
Console.WriteLine(someVariable);
输出:
someInput