C# constructor | Types of constructor

In the earlier tutorial, we discuss class and object in C# . The constructor is a special member function of class. Member functions are those members that provide functionality for manipulating the data in the class. We can declare a method that has the same name as the containing class and does not have any return type even void.

 

There are two types of constructor
• Default constructor


• Custom constructor/ parameterized constructor


C# Static constructor


A static constructor will be executed only once as opposed to the constructor return so far which are instance constructor that is executed whenever an object of that class I created. And use static is a keyword.

 


If a class contains nothing but static method and properties the class itself can become static. A static class is functionally the same as creating a class with a private static constructor and instance of the class can never be created.

Let's create an example of a contractor in C# programming.

C# Custom constructor/ parameterized constructor- 

The parameterized constructor contains the parameters is known the parameterized constructor or custom constructor. It is used to provide a different value to different objects. We can create distinct objects of the class and use different values. 

Let's create an example of C# custom constructor or parameterized constructor.  


using System;

namespace TriangleApplication {

   public class Triangle{
      public double b;   // base of a Triangle
     public double h;   // Height of a Triangle
	   public double ar; 
      
      public Triangle() {
         Console.WriteLine("The Object is being created");
      }

      public void area(double b,double h) 
	  {
         ar = h*b/2;
      }
      
      public double getarea() {
         return ar;
      }

      public static void Main(string[] args) {
         Triangle trngle = new Triangle();    
         
         // Set Triangle base and heigh values 
         trngle.area(2.0,6.0);
         Console.WriteLine("Area of a triangle : {0}", trngle.getarea());
         
      }
   }
}

Output - 

The object is being created
Area of a triangle: 6


In the above C# example, we created a constructor with the same name as the class.

C# Default constructor : Non parameterized constructor -

A default constructor does not contain any argument(s )is known as default constructor or Non parameterized constructor .
It invoked at the time of creating an object.


C# default constructor example- 

 


using System;

namespace RectangleApplication {

   public class Rectangle{
      public double w;   // Width of a Rectangle
     public double h;   // Height of a Rectangle
	   public double ar; 
      
      public Rectangle() {
         Console.WriteLine("The Object is being created");
		  
      }

      public void area() 
	  {
		 w=5.0; 
		  h=3.0;
         ar = w*h;
      }
      
      public double getarea() {
         return ar;
      }

      public static void Main(string[] args) {
         Rectangle rect = new Rectangle();    
         
         
         rect.area();
         Console.WriteLine("Area of Rectangle A=wl: {0}", rect.getarea());
         
      }
   }
}

 

Output -

 
The Object is being created
Area of Rectangle A=wl: 15