Sunday, May 9, 2021

Default Constructor

In this article i will explain what is default constructor of class in c#.

The constructor of the class which do dot have any parameter is called default constructor. In other word this type of constructor does not takes parameters. The disadvantage of default constructor is that every instance of the class will be initialized as the same  values of class variables. So we can not initialized each instance of the class with different values.

The default constructor initializes :

  • All numeric variables to zero.
  • All string variables to null.

 

using System;
                    
public class Program
{
    public static void Main()
    {
        Lab1 l=new Lab1();
        Console.WriteLine("Value of a {0}",l.a);
        Console.WriteLine("Value of name {0}",l.name);
    }
}
public class Lab1{
    public int a;
    public string name;
    public Lab1(){
        a=10;
        name="Brij";
    }
}
 
Output : 
Value of a 10
Value of name Brij 

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...