Thursday 14 February 2013

Constructors:

      It is a special method present under a class which is responsible for initializing the variables of class. The name of constructor method will b same as the class name and more ever it is a non value returning method.

Syntax to define a constructor:
[<modifiers>] <name> ([<parameter defination>])
   {
        Statements;
   }

      Every class requires a constructor to be present in it, if we want to create an object of that class.

Note: As the constructor is mandatory for creating the object of a class, it must be defined by the programmer explicitly, or else while compiling the program compiler takes the responsibility of defining a constructor implicitly under that class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApps
{
    class ConDemo
    {
        public ConDemo()
        {
            Console.WriteLine("Constructor is called");
        }
        public void demo()
        {
            Console.WriteLine("Method is called");
        }
        static void Main()
        {
            ConDemo cd1 = new ConDemo();
            ConDemo cd2 = new ConDemo();
            ConDemo cd3 = cd2;
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment