C#小知識之有趣的類型靜態(tài)構(gòu)造器
這是C#中一個有趣的現(xiàn)象,也許您從中可以窺見些許CLR在構(gòu)造類型時的行為,以及JIT編譯的觸發(fā)式編譯過程。
看下面一段代碼:
class Program
{
static void Main()
{
myValueType1 type1 = new myValueType1();
Console.WriteLine(myValueType1.myInt);
Console.WriteLine("**********************");
myValueType2 type2 = new myValueType2();
type2.myInt =23;
Console.WriteLine(type2.myInt);
Console.WriteLine("**********************");
myValueType3 type3 = new myValueType3();
}
}
struct myValueType1
{
static myValueType1()
{
Console.WriteLine("Hello from myValueType1");
// myInt = 111;
}
public static Int32 myInt;
}
struct myValueType2
{
static myValueType2()
{
Console.WriteLine("Hello from myValueType2");
}
public Int32 myInt;
}
struct myValueType3
{
static myValueType3()
{
Console.WriteLine("Hello from myValueType3");
myInt = 333;
}
public static Int32 myInt;
}
這里定義了三個結(jié)構(gòu):myValueType1,myValueType2,myValueType3。三個結(jié)構(gòu)均帶靜態(tài)構(gòu)造器,在構(gòu)造器中都有一句用來輸出的的代碼。在myValueType1和myValueType3的靜態(tài)。然后我們在main函數(shù)里面分別new 了相應(yīng)的三個實例。您可以先想想輸出的結(jié)果應(yīng)該是怎樣的。
事實上您會得到如下的結(jié)果:
我們看到雖然三個結(jié)構(gòu)中都有靜態(tài)構(gòu)造器,卻只有第一個結(jié)構(gòu)的被執(zhí)行了。事實上,這個有趣的現(xiàn)象也是CLR對性能的考慮,除非類型確實被訪問到了,否則永遠不會調(diào)用到它的類型構(gòu)造器,這個過程是JIT的。
當執(zhí)行到第六行代碼時,CLR嘗試要去myValueType1查找靜態(tài)字段myInt的值。這個時候,myValueType1才是真正被訪問到了。靜態(tài)構(gòu)造器被執(zhí)行,得到相應(yīng)的輸出。
而myValueType2中myInt是個實例成員,訪問它的值只關(guān)系到實例type2實例。與類型本身沒有任何關(guān)系,CLR不會執(zhí)行類型myValueType2的靜態(tài)構(gòu)造器。
myValueType3跟myValueType11幾乎是一樣的,myInt是靜態(tài)成員,但是在main函數(shù)中,myValueType3還是沒有被真正訪問到,只是利用它構(gòu)造出了一個虛擬的對象結(jié)構(gòu),這種對象結(jié)構(gòu)里面所有字段都被賦予一個0值或者null值,所以第二行輸出為零
這些性質(zhì)與JIT編譯器都是分不開的。
相關(guān)文章
C# JavaScriptSerializer序列化時的時間處理詳解
這篇文章主要為大家詳細介紹了C# JavaScriptSerializer序列化時的時間處理詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08C# Dynamic關(guān)鍵字之:解析dynamic就是Object
本篇文章是對C#中dynamic關(guān)鍵字就是Object進行了詳細的分析介紹,需要的朋友參考下2013-05-05