1 回答
TA貢獻1842條經驗 獲得超22個贊
我假設編譯器不會在選項 #1 中緩存委托,但我不確定它是否會在 #2 中緩存。
事實上,在這兩種情況下都可以,而且它們是捆綁在一起的。
來自 ECMA C# 5 規范,第 7.6.10.5 節:
new D(E) 形式的委托創建表達式(其中 D 是委托類型,E 是表達式)的綁定時處理包括以下步驟:
...
如果 E 是匿名函數,則委托創建表達式的處理方式與從 E 到 D 的匿名函數轉換(第 6.5 節)相同。
...
所以基本上兩者的處理方式相同。在這兩種情況下,它都可以被緩存。是的,“新并不一定意味著新”是很奇怪的。
為了說明這一點,讓我們編寫一個非常簡單的程序:
using System;
public class Program
{
public static void Main()
{
var func = new Func<int, bool>(x => x % 2 == 0);
}
}
這是我機器上的方法的 IL Main(誠然是使用 C# 8 預覽版編譯器構建的,但我希望在一段時間內也是如此):
.method public hidebysig static void Main() cil managed
{
.entrypoint
// Code size 29 (0x1d)
.maxstack 8
IL_0000: ldsfld class [mscorlib]System.Func`2<int32,bool> Program/'<>c'::'<>9__0_0'
IL_0005: brtrue.s IL_001c
IL_0007: ldsfld class Program/'<>c' Program/'<>c'::'<>9'
IL_000c: ldftn instance bool Program/'<>c'::'<Main>b__0_0'(int32)
IL_0012: newobj instance void class [mscorlib]System.Func`2<int32,bool>::.ctor(object,
native int)
IL_0017: stsfld class [mscorlib]System.Func`2<int32,bool> Program/'<>c'::'<>9__0_0'
IL_001c: ret
} // end of method Program::Main
這是有效的:
Func<int, bool> func;
func = cache;
if (func == null)
{
func = new Func<int, bool>(GeneratedPrivateMethod);
cache = func;
}
- 1 回答
- 0 關注
- 114 瀏覽
添加回答
舉報
