Constructor

  • Constructor is a member of a class which is a special type of function .
  • Constructor will invoke automatically when an object is created
  • Constructor will not return any value so it will not have return type
  • Class name and Constructor name should be same
  • Constructor can contain parameter
  • Constructor cannot be “virtual”
  • They cannot be inherits
  • Constructor are called in the order of inheritance
  • They can be overloaded

  • Syntax :

    <access modifier><classname()> {
    	//Statement
    }
     

    Program of Constructor

      Namespace ConstructorEx {
    	int a ;
    	int b ;
    	internal  MyClass() {
    		Console.WriteLine(“constructor is calling ”);
    		a=10;
    		b=20;
    	}
    	internal void Display() {
    		Console.WriteLine(“a value is :+a”);
    		Console.WriteLine(“b value is :+b”);
    	}
    }
    Class program {
    	void Main() {
    		Console.WriteLine(“Before object is created ”);
    		MyClass obj=new MyClass();
    		Console.WriteLine(“After object creation”);
    		obj .Display();
    		Console.ReadLine();
    	}
    }
    }
       

    Quantitative Aptitude
    Reasoning
    Programming
    Interview