ठीक है, देखते हैं और देखें कि क्या होता है।
static void MethodGroup()
{
new List<string>().ForEach(Console.WriteLine);
}
static void LambdaExpression()
{
new List<string>().ForEach(x => Console.WriteLine(x));
}
यह निम्नलिखित आईएल में संकलित हो जाता है।
.method private hidebysig static void MethodGroup() cil managed
{
.maxstack 8
L_0000: newobj instance void [mscorlib]System.Collections.Generic.List`1<string>::.ctor()
L_0005: ldnull
L_0006: ldftn void [mscorlib]System.Console::WriteLine(string)
L_000c: newobj instance void [mscorlib]System.Action`1<string>::.ctor(object, native int)
L_0011: call instance void [mscorlib]System.Collections.Generic.List`1<string>::ForEach(class [mscorlib]System.Action`1<!0>)
L_0016: ret
}
.method private hidebysig static void LambdaExpression() cil managed
{
.maxstack 8
L_0000: newobj instance void [mscorlib]System.Collections.Generic.List`1<string>::.ctor()
L_0005: ldsfld class [mscorlib]System.Action`1<string> Sandbox.Program::CS$<>9__CachedAnonymousMethodDelegate1
L_000a: brtrue.s L_001d
L_000c: ldnull
L_000d: ldftn void Sandbox.Program::<LambdaExpression>b__0(string)
L_0013: newobj instance void [mscorlib]System.Action`1<string>::.ctor(object, native int)
L_0018: stsfld class [mscorlib]System.Action`1<string> Sandbox.Program::CS$<>9__CachedAnonymousMethodDelegate1
L_001d: ldsfld class [mscorlib]System.Action`1<string> Sandbox.Program::CS$<>9__CachedAnonymousMethodDelegate1
L_0022: call instance void [mscorlib]System.Collections.Generic.List`1<string>::ForEach(class [mscorlib]System.Action`1<!0>)
L_0027: ret
}
सूचना कैसे विधि समूह दृष्टिकोण एक समय का उपयोग के लिए एक Action<T>
प्रतिनिधि और लैम्ब्डा अभिव्यक्ति दृष्टिकोण बनाता है एक छिपा हुआ गुमनाम प्रतिनिधि क्षेत्र बनाता है और यदि आवश्यक हो तो इसके बारे में एक इनलाइन प्रारंभ करता है। IL_000a
पर brtrue
निर्देश पर ध्यान दें।
स्रोत
2010-10-01 18:56:59
खैर, ReSharper दूसरे संस्करण की सिफारिश की। तो यह सही होना चाहिए ... –
"कोई व्यक्ति समझता है कि सही है" वास्तव में * क्यों * की व्याख्या नहीं है। यह सही हो सकता है। यह सबसे अच्छा जवाब हो सकता है। लेकिन वह "क्यों" के सवाल का जवाब नहीं देता है। – WernerCD