top

Search

C# Tutorial

Constructors are special methods in C# that are automatically called when an object of a class is created to initialize all the class data members. If there are no explicitly defined constructors in the class, the compiler creates a default constructor automatically.Some of the types of constructors are:Default ConstructorA default constructor takes no arguments. Each object of the class is initialized with the default values that are specified in the default constructor. Therefore, it is not possible to initialize the different objects with different values.A program that demonstrates a default constructor is given as follows:Source Code: Program that demonstrates a default constructor in C#using System; namespace DefaultConstructorDemo {   class Sum   {     private int x;     private int y;     public Sum()     {         x = 5;         y = 7;     }     public int getSum()     {         return x + y;     }      }    class Test    {      static void Main(string[] args)      {         Sum s = new Sum();            Console.WriteLine("Sum: {0}" , s.getSum());      }    } }The output of the above program is as follows:Sum: 12Now let us understand the above program.First, the class Sum is initialized. It contains two private variables x and y. The default constructor Sum() initializes x and y to 5 and 7 respectively. Then the function getSum() returns the sum of x and y. The code snippet for this is given below:class Sum   {     private int x;     private int y;     public Sum()     {         x = 5;         y = 7;     }     public int getSum()     {         return x + y;     }    }The main() function is in the class Test. It initializes an object s of class Sum. Then the sum of 5 and 7 is displayed using getSum(). The code snippet for this is given below:class Test    {      static void Main(string[] args)      {         Sum s = new Sum();            Console.WriteLine("Sum: {0}" , s.getSum());      }    }Parameterized ConstructorA parameterized constructor can take one or more parameters. Therefore, it can initialize different class objects to different values. This is an advantage over the default constructor.A program that demonstrates a parameterized constructor is given as follows:Source Code: Program that demonstrates a parameterized constructor in C#using System; namespace ParameterizedConstructorDemo {   class Sum   {     private int x;     private int y;     public Sum(int a, int b)     {         x = a;         y = b;     }     public int getSum()     {         return x + y;     }    }    class Test    {      static void Main(string[] args)      {         Sum s = new Sum(15,9);            Console.WriteLine("Sum: {0}" , s.getSum());      }    } }The output of the above program is as follows:Sum: 24Now let us understand the above program.First, the class Sum is initialized. It contains two private variables x and y. The parameterized constructor Sum() initializes x and y to the values given in a and b respectively. Then the function getSum() returns the sum of x and y. The code snippet for this is given below:class Sum   {     private int x;     private int y;     public Sum(int a, int b)     {         x = a;         y = b;     }     public int getSum()     {         return x + y;     }    }The main() function is in the class Test. It initializes an object “s” of class Sum with the values 15 and 9 provided as parameters for the parameterized constructor. Then the sum is displayed using getSum(). The code snippet for this is given below:class Test    {      static void Main(string[] args)      {         Sum s = new Sum(15,9);            Console.WriteLine("Sum: {0}" , s.getSum());        }    }Copy ConstructorA copy constructor initializes the object values by copying the data from another object. It basically creates a new object which is a copy of the old object.A program that demonstrates a copy constructor is given as follows:Source Code: Program that demonstrates a copy constructor in C#using System; namespace CopyConstructorDemo {   class Sum   {     private int x;     private int y;     public Sum(int a, int b)     {         x = a;         y = b;     }     public Sum( Sum s )     {         x = s.x;         y = s.y;     }     public int getSum()     {         return x + y;     }    }    class Test    {      static void Main(string[] args)      {         Sum s1 = new Sum(2,9);            Sum s2 = new Sum(s1);            Console.WriteLine("Sum of 2 and 9: {0}" , s2.getSum());      }    } }The output of the above program is as follows:Sum of 2 and 9: 11Now let us understand the above program.First, the class Sum is initialized. It contains two private variables x and y. There are two constructors i.e. a parameterized constructor and a copy constructor. The parameterized constructor Sum() initializes x and y to the values given in a and b respectively. The copy constructor copies the values of object s into x and y.Then the function getSum() returns the sum of x and y. The code snippet for this is given below:class Sum   {     private int x;     private int y;     public Sum(int a, int b)     {         x = a;         y = b;     }     public Sum( Sum s )     {         x = s.x;         y = s.y;     }     public int getSum()     {         return x + y;     }    }The main() function is in the class Test. It initializes an object s1 of class Sum with the values 2 and 9 provided as parameters for the parameterized constructor. Then the values of s1 are copied into an object s2 using the copy constructor. Then the sum if x and y in object s2 is displayed using getSum(). The code snippet for this is given below:class Test    {      static void Main(string[] args)      {         Sum s1 = new Sum(2,9);            Sum s2 = new Sum(s1);            Console.WriteLine("Sum of 2 and 9: {0}" , s2.getSum());      }    }
logo

C# Tutorial

Constructors in C#

Constructors are special methods in C# that are automatically called when an object of a class is created to initialize all the class data members. If there are no explicitly defined constructors in the class, the compiler creates a default constructor automatically.

Some of the types of constructors are:

Default Constructor

A default constructor takes no arguments. Each object of the class is initialized with the default values that are specified in the default constructor. Therefore, it is not possible to initialize the different objects with different values.

A program that demonstrates a default constructor is given as follows:

Source Code: Program that demonstrates a default constructor in C#

using System;
namespace DefaultConstructorDemo
{
  class Sum
  {
    private int x;
    private int y;
    public Sum()
    {
        x = 5;
        y = 7;
    }
    public int getSum()
    {
        return x + y;
    }  
   }
   class Test
   {
     static void Main(string[] args)
     {
        Sum s = new Sum();   
        Console.WriteLine("Sum: {0}" , s.getSum());
     }
   }
}

The output of the above program is as follows:

Sum: 12

Now let us understand the above program.

First, the class Sum is initialized. It contains two private variables x and y. The default constructor Sum() initializes x and y to 5 and 7 respectively. Then the function getSum() returns the sum of x and y. The code snippet for this is given below:

class Sum
  {
    private int x;
    private int y;
    public Sum()
    {
        x = 5;
        y = 7;
    }
    public int getSum()
    {
        return x + y;
    }
   }

The main() function is in the class Test. It initializes an object s of class Sum. Then the sum of 5 and 7 is displayed using getSum(). The code snippet for this is given below:

class Test
   {
     static void Main(string[] args)
     {
        Sum s = new Sum();   
        Console.WriteLine("Sum: {0}" , s.getSum());
     }
   }

Parameterized Constructor

A parameterized constructor can take one or more parameters. Therefore, it can initialize different class objects to different values. This is an advantage over the default constructor.

A program that demonstrates a parameterized constructor is given as follows:

Source Code: Program that demonstrates a parameterized constructor in C#

using System;
namespace ParameterizedConstructorDemo
{
  class Sum
  {
    private int x;
    private int y;
    public Sum(int a, int b)
    {
        x = a;
        y = b;
    }
    public int getSum()
    {
        return x + y;
    }
   }
   class Test
   {
     static void Main(string[] args)
     {
        Sum s = new Sum(15,9);   
        Console.WriteLine("Sum: {0}" , s.getSum());
     }
   }
}

The output of the above program is as follows:

Sum: 24

Now let us understand the above program.

First, the class Sum is initialized. It contains two private variables x and y. The parameterized constructor Sum() initializes x and y to the values given in a and b respectively. Then the function getSum() returns the sum of x and y. The code snippet for this is given below:

class Sum
  {
    private int x;
    private int y;
    public Sum(int a, int b)
    {
        x = a;
        y = b;
    }
    public int getSum()
    {
        return x + y;
    }
   }

The main() function is in the class Test. It initializes an object “s” of class Sum with the values 15 and 9 provided as parameters for the parameterized constructor. Then the sum is displayed using getSum(). The code snippet for this is given below:

class Test
   {
     static void Main(string[] args)
     {
        Sum s = new Sum(15,9);   
        Console.WriteLine("Sum: {0}" , s.getSum());  
     }
   }

Copy Constructor

A copy constructor initializes the object values by copying the data from another object. It basically creates a new object which is a copy of the old object.

A program that demonstrates a copy constructor is given as follows:

Source Code: Program that demonstrates a copy constructor in C#

using System;
namespace CopyConstructorDemo
{
  class Sum
  {
    private int x;
    private int y;
    public Sum(int a, int b)
    {
        x = a;
        y = b;
    }
    public Sum( Sum s )
    {
        x = s.x;
        y = s.y;
    }
    public int getSum()
    {
        return x + y;
    }
   }
   class Test
   {
     static void Main(string[] args)
     {
        Sum s1 = new Sum(2,9);   
        Sum s2 = new Sum(s1);   
        Console.WriteLine("Sum of 2 and 9: {0}" , s2.getSum());
     }
   }
}

The output of the above program is as follows:

Sum of 2 and 9: 11

Now let us understand the above program.

First, the class Sum is initialized. It contains two private variables x and y. There are two constructors i.e. a parameterized constructor and a copy constructor. The parameterized constructor Sum() initializes x and y to the values given in a and b respectively. The copy constructor copies the values of object s into x and y.Then the function getSum() returns the sum of x and y. The code snippet for this is given below:

class Sum
  {
    private int x;
    private int y;
    public Sum(int a, int b)
    {
        x = a;
        y = b;
    }
    public Sum( Sum s )
    {
        x = s.x;
        y = s.y;
    }
    public int getSum()
    {
        return x + y;
    }
   }

The main() function is in the class Test. It initializes an object s1 of class Sum with the values 2 and 9 provided as parameters for the parameterized constructor. Then the values of s1 are copied into an object s2 using the copy constructor. Then the sum if x and y in object s2 is displayed using getSum(). The code snippet for this is given below:

class Test
   {
     static void Main(string[] args)
     {
        Sum s1 = new Sum(2,9);   
        Sum s2 = new Sum(s1);   
        Console.WriteLine("Sum of 2 and 9: {0}" , s2.getSum());
     }
   }

Leave a Reply

Your email address will not be published. Required fields are marked *

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial