DEV Community

Sharad Aade
Sharad Aade

Posted on

Constructor in C#🟪

*A constructor is a method in a class named similarly to the class.
which is executed when we create an instance of the class.
*

Image description

NOTE -

  • No return type (not even void)
  • It doesn't return anything.
  • This makes it different from other methods.
  • To initialize class fields(variables) or properties when an object is created means assigning a value to the variables.

Points -

  • 1. Automatically invoked or called when we use the 'new' keyword.

Student s = new Student();

    1. You can have multiple constructors in a class with different parameters(Constructor Overloading).
    1. A constructor with no parameters is called the default constructor
    1. If we not create a manually constructor, then the compiler provides/initialize implicit constructor.

Image description

Top comments (0)