Func<T>
is just a delegate. If it is set to an instance method, then, it [magically] gets the correct this
value when invoked. If set to a static method, then there is no this
. And if it's set to a closure/lambda, the closure "captures" those variables in scope at the time of closure. lexical scope (with some caveats: things don't always work the way you think they might).
Edited to add:
From the C# standard, ISO 23270:2006 Information Technology — Programming Languages — C#, §8.10:
8.10 Delegates
Delegates enable scenarios that some other languages have addressed with function
pointers. However, unlike function pointers, delegates are object-oriented and type-safe.
A delegate declaration defines a class that is derived from the class System.Delegate
.
A delegate instance encapsulates one or more methods, each of which is referred to as
a callable entity. For instance methods, a callable entity consists of an instance
and a method on that instance. For static methods, a callable entity consists of just
a method. Given a delegate instance and an appropriate set of arguments, one can invoke
all of that delegate instance’s methods with that set of arguments.
In more detail, the other standard, ISO 23271:2006 Information Technology — Common Language Infrastructure (CLI) Partitions I to VI says in §14.6:
The instance constructor (named .ctor
and marked specialname
and rtspecialname
,
see §10.5.1) shall take exactly two parameters, the first having type System.Object
,
and the second having type System.IntPtr
. When actually called (via a newobj
instruction, see Partition III), the first argument shall be an instance of the class
(or one of its derived classes) that defines the target method, and the second argument
shall be a method pointer to the method to be called.
It's not that magic.
Func<T>
)? – Haroldharolda