Tuesday, March 13, 2007

"beforeFieldInit"

"beforeFieldInit" is a special flag marked by compiler to the types which doesn't have static constructor.
This special flag tells "calling a static method does not force the system to initialize the type." Means that calling a static method does not make sure the static variables are initialized in the type.
public class ResourceClass
{
private static StreamWriter sw = new StreamWriter();
public static StreamWriter GetInstance()
{
return sw;
}
}
In the above type it is not guranteed that when you call GetInstance the static variable "sw" would be created. Because of the reason the class is marked as "beforeFieldInit".
public class ResourceClass
{
private static StreamWriter sw = new StreamWriter();
static ResourceClass() { };

public static StreamWriter GetInstance()
{
return sw;
}

}

In the above type it is guranteed that when you call GetInstance the static variable "sw" would be created. Because of the reason the class contains "static constructor".
Properties of static constructor
  • Static constructors are not inherited, and cannot be called directly.
The exact timing of static constructor execution is implementation-dependent, but is subject to the following rules:
  1. The static constructor for a class executes before any instance of the class is created.
  2. The static constructor for a class executes before any of the static members for the class are referenced.
  3. The static constructor for a class executes after the static field initializers (if any) for the class. The static constructor for a class executes, at most, one time during a single program instantiation.
  4. The order of execution between two static constructors of two different classes is not specified.
But CLI does insist that all of the field variables will be initialized as soon as one of the static fields is accessed. So if we are depending on some side effects based on static variable initialization, then we may be waiting for a long time that to happen.

52 Comments:

At 11:08 PM, Blogger Vijesh said...

Nice Food for thought!
Can you tell me a real time scenario where this can be used?

 
At 12:26 PM, Blogger Project 71 said...

Hi! My name is Project 71. Weird name I know, but my masters are weird too. My masters apologize for such an out-of-context comment and they know how painful such spamlike comments are. But, say masters, how else are we to present something good to the world. By that they mean me :D. Kindly see what I am about. Won't take you more than 22s to read... http://www.project71.com/readme Enjoyy!

 
At 12:53 AM, Blogger ElroyTLanphear said...

美女視訊18成人聊天室情色小說色情小說貼圖片區情色視訊0204視訊交友百分百成人圖片777成人區摯愛中年聊天室080視訊聊天室0204視訊交友成人視訊視訊做愛av色情影片交友菁英1對1一葉晴貼圖情色小說台灣色情網

 
At 8:18 PM, Blogger 單存 said...

成功是優點的發揮,失敗是缺點的累積 ....................................................

 
At 6:27 AM, Blogger 無尾熊可愛 said...

大奶妹貼圖區0204性影片觀賞露點自拍淫婦女生如何自慰色情站成人笑話av激情網愛視訊美女淫蕩av成人色情電話辣妹視訊聊天性關係情色vcd自慰圖淫美成人論壇台灣色情論壇成人聊天室自拍裸女貼圖視訊成人免費a片影片av成人網成人色情色情台灣辣妹小穴太太陰毛色情訊息裸女自拍色情影片a片論壇性愛技巧美女脫胸罩情色天堂av寫真色情視訊聊天做愛視訊成人影片床上戲情色聊天網火辣情色台灣女優性愛秘笈台灣av女優手淫自慰影片

 
At 6:27 AM, Blogger 龐克搖滾 said...

任何事都是由一個決心,一顆種子開始。........................................

 
At 6:22 PM, Blogger 宥軒 said...

偉大的致富萬能之鑰,正是幫你充分掌握自己心志所必須的自律自制........................................

 
At 1:20 AM, Blogger 佳玲曉豪 said...

我們唯一需要恐懼的事,是恐懼本身........................................

 
At 3:05 AM, Blogger 雅琳 said...

hello~welcome my world~<. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 
At 7:20 PM, Blogger 賢諭 said...

無一事而不學,無一時而不學,無一處而不學。......................................................

 
At 3:34 AM, Blogger TammySteffes2217 said...

過去的事早已消失,未來的更是渺不可知,只有現在是真實的。........................................

 
At 11:21 AM, Blogger BlancaMcleroy1230 said...

sex女,ut,貼影,av,高潮,女優,做愛,手機成人影片,色遊戲,成人動漫,百分百貼圖區,85cc影片,成人影音,av色情影片,A片線上,a片,sex,777,三級線上看,美女的照片,視訊做愛,78論壇,打飛機,免費視訊,成人影院,辣妹視訊,視訊聊天,影片網,kiss911,a片,交友,聊天,做愛,免費影片,性交,線上成人,網路色情,聊天,美女自慰,免費a分享,免費短片,裸照,一夜情,女優,85cc成人片,美女寫真,偷拍a,情人視訊聊天室

 
At 5:09 AM, Blogger 嘉花 said...

Nice post ~ 3Q..............................................................

 
At 11:12 PM, Blogger Angelo冠宇 said...

After a storm comes a calm..............................................

 
At 6:38 PM, Blogger M12aeganT_Moe12 said...

凡是遇到困擾的問題,不要把它當作可怕的,討厭的,無奈的遭遇,而要把它當作歷練、訓練和幫助。.............................................

 
At 2:41 AM, Blogger 志文 said...

裸體裸照裸女愛愛無碼尋夢視訊聊天a漫a片a圖一夜情一葉情人妻激情情色寫真美女自拍辣妹自拍正妹自拍美女走光辣妹走光正妹走光脫衣秀脫衣走光色情自慰自拍成人全裸打炮打手槍打飛機巨乳巨奶女優

 
At 8:03 PM, Blogger 柏懿綺辰 said...

Time and tide wait for no man. ............................................................

 
At 2:21 AM, Blogger 佩GailBohanan1蓉 said...

你要保守你的心,勝過保守一切,因為一生的果效是由心發出........................................

 
At 6:56 PM, Blogger 趙佳治 said...

不只BLOG內容很棒留言也很精采 XDDDD.........................

 
At 7:00 AM, Blogger 嘉琬嘉琬 said...

you two make a lovely couple!............................................................

 
At 2:55 AM, Blogger 江婷 said...

成熟,就是有能力適應生活中的模糊。.................................................................

 
At 7:19 AM, Blogger 峻龍 said...

河水永遠是相同的,可是每一剎那又都是新的。......................................................................

 
At 1:58 AM, Blogger 韋志韋志 said...

成熟,就是有能力適應生活中的模糊。.................................................................

 
At 4:51 AM, Blogger 筱婷筱婷 said...

人有兩眼一舌,是為了觀察倍於說話的緣故。............................................................

 
At 6:50 PM, Blogger 云依恩HFH謝鄭JTR安 said...

初次造訪,安安^^..................................................................

 
At 6:26 PM, Blogger 云依恩HFH謝鄭JTR安 said...

看到大家都留言-我也忍不住說聲---加油..................................................................

 
At 6:28 PM, Blogger 有年 said...

很精彩的部落格 期待你的繼續加油..................................................................

 
At 10:49 PM, Blogger 婷珊 said...

要經常發表文章 最愛你了呦............................................................

 
At 7:57 AM, Blogger 楊儀卉 said...

Make hay while the sun shines.............................................................

 
At 10:30 PM, Blogger 宋瑞正宋瑞正 said...

你不能決定生命的長度,但你可以控制它的寬度............................................................

 
At 6:52 AM, Blogger 陳佑發 said...

Never put off till tomorrow what may be done today.............................................................

 
At 12:54 AM, Blogger 琬安琬安 said...

優質的好部落格,需要大家的支持!............................................................

 
At 10:05 PM, Blogger 謝文新李怡君 said...

天助自助者~~大家一起加油!為了更好的將來!............................................................

 
At 10:28 PM, Blogger 正洪玲正洪玲正洪玲 said...

不要去想沒拿到的東西,多想想自己手裡所擁有的..................................................

 
At 10:34 PM, Blogger 謝文新v謝文新 said...

良言一句三冬暖,惡語傷人六月寒。......................................................................

 
At 2:04 AM, Blogger GusF_Finkbeiner1蕙帆 said...

拒絕冒險和成長的人,終將被生命的潮流陶汰。..................................................

 
At 1:44 AM, Blogger 孟湖聿軒 said...

人生像一杯茶,若一飲而盡,會提早見到杯底.......................................................

 
At 7:59 PM, Blogger 韓志張人豪豪 said...

成功可招引朋友,挫敗可考驗朋友............................................................

 
At 8:28 AM, Blogger 46854 said...

你不能改變容貌~~但你可以展現笑容.................................................................

 
At 8:53 PM, Blogger 孫邦柔 said...

當一個人內心能容納兩樣相互衝突的東西,這個人便開始變得有價值了。............................................................

 
At 9:59 PM, Blogger 幸平平平平杰 said...

有夢最美啦~~加油!元氣滿點!...............................................................

 
At 1:32 AM, Blogger  said...

如果你批評他人。你就沒有時間付出愛......................................................................

 
At 1:32 AM, Blogger 文滢 said...

唯有用熱情、用智慧去觀察事物,這事物才會把他的秘密,洩漏給我們......................................................................

 
At 8:21 AM, Blogger 孟湖聿軒 said...

死亡是悲哀的,但活得不快樂更悲哀。. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 
At 11:26 PM, Blogger 万一宇 said...

支持大大~很有啟發性......................................................

 
At 1:29 AM, Blogger Jeeshi said...

It's a important sites for contents and different kinds of information of the course in the blogs. Thanks for sharing.

hadoop training

 
At 1:44 AM, Blogger Shalini said...

Superb i really enjoyed very much with this article here. Really its a amazing article i had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your updation.
Digital
Marketing Company in Chennai

 
At 10:06 PM, Blogger Jeffy said...

Thank you for sharing such a nice and interesting blog with us. I have
seen that all will say the same thing repeatedly. But in your blog, I had
a chance to get some useful and unique information. I would like to
suggest your blog in my dude circle. please keep on updates. hope it
might be much useful for us. keep on updating...
seo company in chennai

Digital Marketing company in chennai

 
At 11:23 PM, Blogger Priya said...

The given information was very excellent & Great tips, and awesome way to get exert tips from everyone, not only i like that post all peoples like that post, because of all given information was wonderful and it's very helpful for me.
IELTS Coaching in Chennai | IELTS Training in Chennai | IELTS Coaching Centre in Chennai

 
At 5:06 AM, Blogger Vignesh PV said...

your information is really awesome as well as it is very excellent and i got more interesting information from your blog.
ios Training in Chennai

 
At 11:55 PM, Blogger Shalini said...

Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.
Digital Marketing Company in India
Seo Company in India

 
At 3:18 AM, Blogger john stany said...


You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...

Android App Development Company

 

Post a Comment

<< Home