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 *

Comments

kariya arti

very satisfied!!

jean-Francois Michaud

Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).

Kshitiz

Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz

Ed

The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation

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