ECMA-334: 12.3.3.27 Anonymous method expressions
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
12.3.3.27 Anonymous method expressions
The definite assignment state of a parameter of an anonymous method (§14.5.15) is the same as for a parameter of a named method. That is, reference parameters and value parameters are initially definitely assigned and output parameters are initially unassigned. Furthermore, output parameters shall be definitely assigned before the anonymous method returns normally (§12.1.6).
The definite assignment state of an outer variable v on the control transfer to the block of an anonymous-method-expression is the same as the definite assignment state of v before the anonymous-method-expression. That is, definite assignment of outer variables is inherited from the context of the anonymous-method-expression. Within the block of an anonymous-method-expression, definite assignment evolves as in a normal block (§12.3.3).
The definite assignment state of a variable v after an anonymous-method-expression is the same as its definite assignment state before the anonymous-method-expression. [Example: The example
delegate bool Filter(int i); void F() { int max; // Error, max is not definitely assigned Filter f = delegate(int n) { return n < max; }; max = 5; DoWork(f); }
generates a compile-time error since max is not definitely assigned where the anonymous method is
declared. end example] [Example: The example
delegate void D(); void F() { int n; D d = delegate { n = 1; }; d(); // Error, n is not definitely assigned Console.WriteLine(n); }
also generates a compile-time error since the assignment to n in the anonymous method has no affect on the
definite assignment state of n outside the anonymous method. end example]