Can A Class Work Without Constructor In C#

What is Constructor?

 
A special method of the class that is automatically invoked when an instance of the class is created is called a constructor. A constructor is a special method that is used to initialize objects. The advantage of a constructor, is that it is called when an object of a class is created.
 
When we create instance of a class a default constructor  is invoked automatically having the same name as Class . Some key points,
  • Constructor is used to initialize an object (instance) of a class.
  • Constructor is a like a method without any return type.
  • Constructor has the same name as class name.
  • Constructor follows the access scope (Can be private, protected, public, Internal and external).
  • Constructor can be overloaded.
Following is the list of constructors in C#.
  • Default constructor
  • Parameterized constructor
  • Private Constructor
  • Static Constructor
  • Copy Constructor
Default Constructor
 
A constructor without any parameters is called a default constructor. If we do not create constructor the class will automatically call default constructor when an object is created.
 
Example of Default Constructor,
  1. using System;  
  2. namespace DefaultConstractor {  
  3.     class addition {  
  4.         int a, b;  
  5.         public addition() //default contructor  
  6.         {  
  7.             a = 100;  
  8.             b = 175;  
  9.         }  
  10.         public static void Main() {  
  11.             addition obj = new addition(); //an object is created , constructor is called  
  12.             Console.WriteLine(obj.a);  
  13.             Console.WriteLine(obj.b);  
  14.             Console.Read();  
  15.         }  
  16.     }  
  17. }  
Parameter Constructor
 
A constructor with at least one parameter is called a parameterized constructor.
 
Private Constructor

Private constructors are used to restrict the instantiation of object using 'new' operator. A private constructor is a special instance constructor. It is commonly used in classes that contain static members only.
 
Static Constructor
 
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced
 
Copy Constructor
 
The constructor which creates an object by copying variables from another object is called a copy constructor.
 
Is it possible to have a class without Constructor?
 
If a class contains no instance constructor declarations, a default instance constructor is automatically provided. The only classes in C# which don't have any instance constructors are static classes, and they can't have constructors.
 
Example
  1. class Person {  
  2.     //This class has no constructor  
  3.     public void getPersonProfile() {  
  4.         Console.WriteLine("Base class:getPersonProfile() have no constructor");  
  5.     }  
  6. }  
  7. class Program {  
  8.     static void Main(string[] args) {  
  9.         //Create object of person  
  10.         Person p = new Person();  
  11.         p.getPersonProfile();  
  12.     }  
  13. }  

Summary

 
In this article, I discussed about constructors in C# language, I hope you found this article useful .
 
Thank you !