Thursday, 3 May 2018

what is Constructor in C#?

What is Constructor in C# Programming?

IT is a special type of method which have same name ass the name of the class. Without constructor an object can not be created so  every class must have at least one constructor.
For creating object , program always execute constructor. It can also be used to store values in the objects.
Constructor executed automatically. It can be parameterized as well as non parameterized.

a-What is Parameterized Constructor in C#:
  
Those constructor that accept the value before execution are known as parameterized constructor.
Parameterized constructor should be define in the class to store value in the object.

b-Non Parameterized constructor:

Those constructor that do not accept value are known as non parameterized constructor. Non parameterized constructor is automatically created by the compiler in the class.

Example:
Class Circle
{
int radius;
public Circle(int r)
 {
radius=r;
}
public void findarea()
{
double res=3.14*radius*radius;
Console.WriteLine(res);
}
}
________________________________________________

Detail Explaination of Constructor program in C#


  what is New operator:

It is given to create an object.
New operator allocate memory to the object(memory to fields of object) dynamically.
new Circle(25);

 What is Reference? : 

Every object of a C# language program has a unique reference by which that object can be accessed so if you want to execute any method on the object then you need to give reference of that object to the method. It is mandatory .

What is Reference Variable:

Reference variable are used to store reference of the object.
The data type of reference variable should always be that class which object is created.
Circle c1=new Circle(25);
Circle c2=new Circle(14);
c1.findarea();
Reference of an object can be kept in multiple reference variable.

What is Properties?:

In C# properties are special types of method which can be defined in the class to set and get value to an object.
It means by using properties of the object you can changed the values of fields of the object. as well as you can get the values of fields also.

Example:
Class circle
{
int radius;
Public void findarea()
{
double res=3.14 * radius *radius;
Console.writeLine(res);
}
Public int radius
{
Get
{
Return radius;
}
Set
{
Radius=value’
}
}
}
Static void main(string []args)
{
circle c1=new circle();
c1.radius=10;
c1.findarea();
int ra =c1.radius;
Console.write(ra);
}

..........................................................

Thanks for being with us make sure you share and stay with us with subscribing us -



 Regards

 CODERBABA

...........................

Follow Me Socially :

Youtube: https://www.youtube.com/coderbaba

Facebook : https://www.facebook.com/coderbaba

 Instagram :https://www.instagram.com/coderbaba

 Twitter : https://twitter.com/thecoderbaba


............................



See you in next blog, have a nice day.


GOOD LUCK

Thank you for visiting
  .............................................

No comments:

Post a comment

Popular Posts