C# Constructor1 min read

In C#, a constructor is the member of the class and is automatically invoked once the object of the class is created. It is like a method in a program and is used for the initialization of the data members of a newly created object.

Two types of C# constructors

  1. Default constructor
  2. Parameterized constructor

Default Constructor in C#

A constructor without any parameters is called a default constructor and is invoked at the time of creating the object of the class.

Example: Default C# Constructor

Output:

Constructor Called
Constructor Called

As you can see, that the constructor is called every time we create a new object of the class. And since it is a default constructor, it has no parameter.


Parameterized Constructor in C#

A Constructor having a specific number of parameters is called Parameterized Constructor. Passing parameters is useful when you want to assign a different initial value to distinct objects.

Example: Parameterized C# Constructor

Output:

1101 Shaun 80.5
1102 Garen 90

As you can see the student details are initialized and displayed for every object created for the Student class in a program.


MORE

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …
Read More

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More