You may recall that you can subscribe to events by doing this:
something.SomeEvent += SomeHandler;
That's syntactic sugar for:
something.SomeEvent += new EventHandler(SomeHandler);
This creates a new EventHandler
instance (EventHandler
is a delegate type), which uses the method SomeHandler
.
Something similar is going on here.
list.Any
takes a Func<T, bool>
, which is a delegate type. Therefore you need to pass it an instance of Func<T, bool>
.
When you write:
list.Any(x => current.Contains(x))
the compiler creates a new method (which takes a string
, returns a bool
, and just calls current.Contains
), and does something like this:
list.Any(new Func<string, bool>(GeneratedMethod))
Now, you can also create that Func<string, bool>
delegate instance from an explicit method yourself, just as in the EventHandler
example:
list.Any(new Func<string, bool>(current.Contains))
Or, you can leave off the new Func<string, bool>
and the compiler will generate it for you, again just as in the earlier example:
list.Any(current.Contains)