O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

7.3. Instance constructors

Instance constructors in C# are simply Java constructors. The reason for the name is because there is another group of constructors in C# called static constructors (see section 7.4).

Here is an example of a class with overloaded instance constructors.

 1: using System;
 2:
 3: public class Test{
 4:   public static void Main(){
 5:     Test t1 = new Test();
 6:     Test t2 = new Test("Here is a string");
 7:   }
 8:
 9:   // overloaded instance constructors
10:   public Test(){
11:     Console.WriteLine("Running default constructor");
12:   }
13:   public Test(string i){
14:     Console.WriteLine("Running constructor with param");
15:   }
16: }

Output:

c:\expt>test
Running default constructor
Running constructor with param

Like Java

  • Instance constructors ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required