The constructor is automatically called whenever you create an object. - CSharp Custom Type

CSharp examples for Custom Type:Constructor

Description

The constructor is automatically called whenever you create an object.

Demo Code

using System;//  w ww . j a  v a  2s.co m
public class myClass
{
   static public int si;
   public int i;
   public void routine()
   {
      Console.WriteLine("In the routine - i = {0} / si = {1}\n", i, si );
   }
   public myClass()
   {
      i++;
      si++;
      Console.WriteLine("In Constructor- i = {0} / si = {1}\n", i, si );
   }
}
class TestApp
{
   public static void Main()
   {
      Console.WriteLine("Start of Main function...");
      Console.WriteLine("Creating first object...");
      myClass first = new myClass();
      Console.WriteLine("Creating second object...");
      myClass second = new myClass();
      Console.WriteLine("Calling first routine...");
      first.routine();
      Console.WriteLine("Creating third object...");
      myClass third = new myClass();
      Console.WriteLine("Calling third routine...");
      third.routine();
      Console.WriteLine("Calling second routine...");
      second.routine();
      Console.WriteLine("End of Main function");
   }
}

Result


Related Tutorials