I want to create a delegate type in C# inside a method for the purpose of creating Anonymous methods.
For example:
public void MyMethod(){ delegate int Sum(int a, int b); Sum mySumImplementation=delegate (int a, int b) {return a+b;} Console.WriteLine(mySumImplementation(1,1).ToString()); }
Unfortunately, I cannot do it using .NET 2.0 and C# 2.0.
Why do you want to create the delegate type within the method? What’s wrong with declaring it outside the method? Basically, you can’t do this – you can’t declare a type (any kind of type) within a method.
One alternative would be to declare all the Func/Action generic delegates which are present in .NET 3.5 – then you could just do:
The declarations are on my C#/.NET Versions page.