I have this code below.
delegate void TestDel(string str);
static void Main(string[] args)
{
TestDel td = name=> Console.WriteLine(name);
TestDel td = (string name) { Console.WriteLine(name);}
td("hello");
Console.ReadLine();
}
Here I have a delegate TestDel , which is created first by using a lambda expression which goes fine.
But the second method where I am using an anonymous method it doesn’t compile but if do like below:
TestDel td = delegate(string name) { Console.WriteLine(name);};
Then everything is fine, my confusion is that why can I use lambda expression and not anonymous method while they are same , why do i need to put delegate with anonymous method but not with lambda expression ?
Your second example should be: