It depends. In the current version of Visual Studio, methods that implement lambdas are never public, but they are not always private. A simple program to check some versions of lambdas:
public class Program { public static void Main() { var program = new Program(); Try("A", program.A); Try("B", program.B); Try("C", program.C); Console.ReadKey(); } private static void Try(string name, Func<Action> generator) { var mi = generator().Method; Console.WriteLine($"{name}: DeclaringType={mi.DeclaringType}, Attributes={mi.Attributes}"); } private Action A() => () => { }; private Action B() => () => { ToString(); }; private Action C() { var c = 1; return () => c.ToString(); } }
prints
A: DeclaringType=Scratch.Program+<>c, Attributes=PrivateScope, Assembly, HideBySig B: DeclaringType=Scratch.Program, Attributes=PrivateScope, Private, HideBySig C: DeclaringType=Scratch.Program+<>c__DisplayClass4_0, Attributes=PrivateScope, Assembly, HideBySig
A lambda has no captures. It is created as an internal method for an empty closure class.
B lambda captures this . It is created as a private method of the containing class.
C lambda captures C It is created as an internal method of a nonempty closure class.
All of this is undocumented and has changed in the past, so it would be nice not to rely on it. The important thing is that when you invoke an anonymous method, it behaves as directed. If you need something more, you should not use anonymous methods. Depending on what you need, you can also use lambdas, but with expression trees, or you may need to create regular named methods.
hvd Mar 07 '16 at 9:55 2016-03-07 09:55
source share