Constructors?

Constructor:
Constructor looks like a method
constructor may not have return type
constructor name same name as the class name
constructor will be used while creating the object
with out creating object we cant create object
"Constructor is a place for defining Business Rules these rules should be applied while creating the object"

Types of Constructors:
we have 3 types of constructors
  • Private Constructor
  • Static Constructor
  • Copy Constructor
Private Constructor:
It is a special instance constructor
To initialize the static data
If a class has one or more private constructors and their is no public constructors then other class(Except Nested class)are not allowed to create instance of this calss
 Ex:
public sealed class PrivateConstuctor
    {
        public static readonly PrivateConstuctor Instance = new PrivateConstuctor();
        public int A;
        private PrivateConstuctor()
        {
            this.A = 5;

        }
        class TestProgram
        {
            static void Main()
            {
                PrivateConstuctor obj = PrivateConstuctor.Instance;
                Console.WriteLine(obj.A);
                obj.A++;
                Console.WriteLine(obj.A);


            }
        }
    }
OutPut:
5
6

Static Constructor:
It is used to initialize any static data
To perform a particular action that needs to be performed once only
It is called automatically before the first instance is created or any static members are referenced
static constructors may not have parameters
their is no access modifier to define the static constructor
it cannot  be called directly
it is useful when creating wrapper classes for unmanaged code,when the constructor can call LoadLibrary method


public class Bus
{
    // Static constructor:
    static Bus()
    {
        System.Console.WriteLine("The static constructor invoked.");
    }

    public static void Drive()
    {
        System.Console.WriteLine("The Drive method invoked.");
    }
}

class TestBus
{
    static void Main()
    {
        Bus.Drive();
    }
} 
 
OutPut:
The static constructor invoked.
The Drive method invoked.

Copy Constructor:
If we want to create a new object and want to copy the values from existing object is called copy constructor

class Person
    {
        private string name;
        private int age;

        public Person(Person PreviousPerson)
        {
            name = PreviousPerson.name;
            age = PreviousPerson.age;

        }
        public Person(string name, int age)
        {
            this.name = name;
            this.age = age;

        }
        public string Details
        {
            get
            {
                return name + " is " + age.ToString();
            }

        }
    }
    class CopyConstructor
    {
        static void Main()
        {
            Person person1 = new Person("Santhosh",25);

            Person person2 = new Person(person1);
            Console.WriteLine("Person2 Details:" +person2.Details);

        }
    }

OutPut:
Person2 Details: Santhosh is 25
 
 

No comments:

Post a Comment