Jan 9, 2020

WHY CONSTRUCTOR IS NEED IN OUR CLASS C# PROGRAMMING

WHY CONSTRUCTOR IS NEED IN OUR CLASS C# PROGRAMMING


            We already know about constructor what is it and how can we use it the need of constructor in our class due to following reasons.
advantages of constructor in c#, c# constructor, constructor,  use of constructor in programming, class member in programming
why constructor is need to our class in c#
1.    The very important first need of constructor is, every class requires a constructor in that class if we want to create the instance of that class. Lucky every class has its own constructor implicitly by the compiler if the constructor is not defined explicitly.
2.    The second point that comes our mind that what the need of explicit constructor if already that class have implicit constructor. The answer is because of the implicit constructor initialize the instance with default/same value even we create multiple instances in that class. But most time we need different value by changing instance. There for implicit constructor also known as default constructor.
3.    Now the third point the advantage of explicit constructor, is if we defined parametric constructor explicitly (a constructor that take argument) then we get a chance to change of initializing field of the class with new values every time to create instance of that class. Like this
public class test
{
  public int x;
  public test (int x)
  {
    this. x = x;   //this x refer to class variable x and x refer to the coming           
                         argument / local variable.
  }
}
Now when we create instance of the class we have opportunity to give different values with the time we create its instance. If the class needs a value then always try to create an explicit constructor.
4.    This point should note that whenever we defined a class, then every class need a value to execute that value can pass through the constructor only.

What is class member in c#      Constructor overloading    Static constructor



0 comments:

Post a Comment

Please do not enter any spam link in the comment box