I am reading about anonymous methods and am trying to wrap my head around this example:
List<int> evenNumbers = list.FindAll(delegate(int i)
{ return (i % 2) == 0; } )
Why is delegate(int i)
legal? You aren't having to declare new delegate void
or anything like that.
Is that what is meant by anonymous method? Is this the added syntactic sugar that allows for anonymous methods?
It's legal because of what you suspect, it's creating an anonymous delegate/method.
An alternative (using the lambda operator =>) would be:
List<int> evenNumbers = list.FindAll((i) => ((i % 2) == 0));
or
List<int> evenNumbers = list.FindAll(i => i % 2 == 0);
See Lambda Expressions for further reading.