C# Private Constructor

C# Private Constructor

Private constructor: In C#, A parameterless constructor with a private access modifier in a class is known as a private constructor. It is a special instance constructor which is mainly used to control the instantiation of a class.

The main intension behind private constructor is to control the object creation and to stop explicit inheritance.

Continue reading “C# Private Constructor”

Copy Constructor in C#


In my previous post, I have explained about the Default constructorParameterized constructor, Static constructor and Private Constructor.

Now in this post, I’ll explain in details about the copy constructor with example.

Copy Constructor in C#

C# does not provide a copy constructor for the objects but as a developer, we can write one by ourselves based on our requirement.

Copy constructor: In C#, A parameterized constructor that takes the same type of object as a parameter is called a copy constructor.

The copy constructor is useful when we want to copy data from the existing object to the new object without affecting the values of the existing object.
In simple words, copy constructor means cloning of data from one object to the other object.

Note: To create a copy constructor we must have more than one constructor in a class.

Continue reading “Copy Constructor in C#”

Static Constructor in C# with Examples


In the previous post, We have discussed about the Default constructor, Parameterized constructor, and Copy constructor.

Now in this post, We will learn about the static constructor with examples in C#.

C# Static Constructor

A  static  constructor is a special kind of constructor that invokes automatically before the first instance of a class is created.
It is used to initialize only static data members of the class or to write the code that needs to be executed only once throughout the execution of the program.

The purpose of using a static constructor is to trigger a piece of code automatically without creating an object of a class.

Static Constructor Syntax

 // Class
    public class Customer
        // Static Constructor
      static Customer()
            // Code

In the above code syntax, a static constructor is created without any parameter and access modifier.

Continue reading “Static Constructor in C# with Examples”

Constructors in C# with Examples

What is a Constructor in C#?

In C#, Constructors are special types of methods that get executed when an instance of the class or struct is created.

The main use of the constructor is to initialize and set the default values for the data members of the new object. It looks very similar to a method, but with no return type and have the same name as the class or struct.

A constructor is invoked when we use the new operator or the reflection (Example: Activator.CreateInstance) to create an object of a class.

If we forget to define the default constructor in a class, the compiler will automatically create one system-defined default constructor for that class.

C# Constructor Syntax

Syntax to create a constructor in C#

constructor syntax

In the above example, we have created constructor “User” with the same name as class or struct.

Continue reading “Constructors in C# with Examples”