-3

How would I rewrite the below C++ code into 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;});

I have tried playing with delegates but I am not quite understanding it yet.

Pita
  • 111
  • 7
  • Your lambda in c# would look like this: `(string input) => { someVariable = input; }` or for brevity `(x) => someVariable = x`. – gmiley Dec 01 '15 at 16:20

1 Answers1

1

Not sure why you'd want to do it like this but here's the equivalent C# code:

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

Output:

someInput

Demo: https://ideone.com/03sbqH

Marco Fatica
  • 937
  • 7
  • 12