Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Lambda expression C# 2.0

Tags:

c#

How to convert expression

v => names.Add(v); // Consider names as List<string> and v as string 

into c# 2.0?

like image 332
csLijo Avatar asked Sep 23 '26 16:09

csLijo


1 Answers

You can use an anonymous method instead:

Action<String> action = delegate (string v) { names.add(v); };

That will work if the existing lambda expression is being converted into a delegate. It won't work when the existing lambda expression is being converted into an expression tree.

(Do you really need to use C# 2 though? Eek. Don't forget that you can use C# 3 or higher but still target .NET 2, if that's the real requirement. The benefits in productivity for using C# 3 and higher really make it work upgrading your version of Visual Studio...)

like image 159
Jon Skeet Avatar answered Sep 25 '26 07:09

Jon Skeet