Constructor class in C#

Now we are going to learn about the Constructor in C#.
  • 4125

Introduction

A constructed class is combination of the class and structure in programming language. Structure is user defined data type which hold the property of the class as like the constructor indexer, method, field, nested type etc.

Constructor are used in programming language to set the default value. Another use of constructor is to make program easier to read for user. There is some difference between the class and structure is that a class can inherit from another class but structure can not be inherit from the another structure.

In short we can say that constructor called when a class is created by us and a method is automatically linked with the class when we do not create a constructed compiler it automatically create a default constructor.

Example of the constructed class

public struct Point
{
public
int x, y;
public
Point(int x, int y)
{
this.x = x;
this
.y = y;
}
}
class MainClass
{
public
static void Main()
{
Point myPoint;
myPoint.x = 10;
myPoint.y = 20;
Console.WriteLine("My Point:");
Console
.WriteLine("x = {0}, y = {1}", myPoint.x, myPoint.y);
Console
.Read();
}
}
}

Output

contru.jpg

© 2020 DotNetHeaven. All rights reserved.