C# Constructors

A Constructor in C# has the same name as the class name. Constructor gets called automatically when we create an object of a class. It does not have a return value. In this lesson, we will learn what are Constructors with examples and how to set parameters.

We have discussed the following topics:

  • How to create a Constructor
  • Constructor Parameters

How to create a Constructor

To create a Constructor in C#, the same class name is used followed by parentheses. Let’s say the class name is Studyopedia, therefore the Constructor would be the following:

Let us see an example to create a Constructor in C#:

Output

Constructor Parameters

Parameterized Constructor is a constructor with a particular number of parameters. This is useful if you have different objects and you want to provide different values.

Let us see an example of a parameterized constructor with 2 parameters:

Output

C# Destructors
C# Classes and Objects
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading