如何使用C#在映射容器中编写带有lambda表达式的std::函数
How to write a std::function with lambda expression in a map container using C#?
如何将下面的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;});
我试过和代表们一起玩,但我还不太理解。
不确定为什么要这样做,但这里有等效的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
演示:https://ideone.com/03sbqH
相关文章:
- 这 4 个 lambda 表达式之间有什么区别?
- 使用成员在类中创建 lambda 表达式
- 将 lambda 表达式传递给 std::function in C++
- 在 lambda 表达式中使用 std::atomic
- &&对lambda表达式有什么好处?
- Tbb 库:错误:编写自定义类函数而不是 lambda 表达式时,对函数的调用不匹配
- 列表.erase 中的 lambda 表达式
- 使用 lambda 表达式的 Raspbian G++ 8.3.0 导致 ']' 之前的预期主表达式 - 即使标准设置为 c++14
- 仅通过引用捕获的 lambda 表达式是否保证不会抛出?
- C++: priority_queue:模板参数中的 lambda 表达式
- 容器如何处理 lambda 表达式的参数
- 在C++ Lambda 表达式中,为什么人们更喜欢按值捕获而不是作为参数传递?
- 如何在 lambda 表达式中传递变量?
- 针对 std::function 的 lambda 表达式和模板推导:为什么会这样?
- 如何修改Lambda表达式以将输出放入文本文件
- C++如何使用lambda表达式来捕获上一次迭代的值
- 为什么Qt在信号和插槽中为lambda表达式抛出错误?
- C++:从捕获函数参数的函数返回 lambda 表达式
- C++ Lambda 表达式:通过 ref 开销捕获
- Qt 连接无法识别 lambda 表达式