Posts

Showing posts from August, 2010

Revise C#'s Constructor Overloading

When overloading constructor in C# I always tends to forgot what is the execution cycle during runtime. So I made a quick program with LinqPad to refresh my mind. 1st I create a simple class Item with multiple constructor overloading each others. class Item { private int i = 1 ; public Item (){ this .i = i* 10 ; Console.WriteLine( "Item - constructor 1 executed. i = " + this .i); } public Item ( int i) : this (){ this .i *= i; Console.WriteLine( "Item - constructor 2 executed. i = " + this .i); } public Item ( string a) : this ( 5 ){ Console.WriteLine( "Item - constructor 3 executed. i = " + this .i); } } then I create a Main function to create new Item. // Constructor Overloading void Main () { Console.WriteLine( "START" ); Console.WriteLine( "--------------" ); var i = new Item(); Console.WriteLine( "--------------" );