Saturday, April 21, 2012

Difference between Static class and Private constructor class


Difference between Static class and Private constructor class

A static constructor is called before the first instance is created. So its kind of global initializer. Whereas Private constructor is called after the instance of the class is created. Inheritance-wise both are same.
The static constructor will only be executed once.  The private constructor will be executed each time it is called.
The static constructor cannot have parameters.  The private constructor can have any parameters you want.
A static class can only have static members and cannot be subclassed.  A public class can have instance or static members and can be subclassed.
so we can not initialize the variable in the private constructor,  the only one use is to restrict the object creation of the class correct.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OOPConcepts
{
    class Program
    {
        static void Main(string[] args)
        {
            OnlyOne.SetMyName("I m the only one."); //static constructor will be called first time when the class will be referenced.
            Console.WriteLine(OnlyOne.GetMyName());

            NoInstance.SetMyName("I have private constructor"); //No constructor will be called when the class will be referenced.
            Console.WriteLine(NoInstance.GetMyName());

            Console.Read();
        }
    }

    static class OnlyOne
    {
        static string name;
        ///

        /// This will be called first time when even the class will be referenced.
        ///

        static OnlyOne()
        {
            name = string.Empty;
            Console.WriteLine("Static constructor is called");
        }

        public static string GetMyName()
        {
            return name;
        }

        public static void SetMyName(string newName)
        {
            name = newName;
        }
    }

    public class NoInstance
    {
        static string name;
        private NoInstance()
        {
            name = string.Empty;
            Console.WriteLine("No instance private constructor is called");
        }

        public static string GetMyName()
        {
            return name;
        }

        public static void SetMyName(string newName)
        {
            name = newName;
        }
    }
}


Private Constructor
And if we mark the constructor as private we wont be able to write
class1 obj=new Class1()
hence the object of class1 cannot be created.
this type of construction is required when we are working with signleton pattern (Design Pattern)

Static Constructor
When we want our static data should be initiallize only one and first time we use
static constructor.
The Static constructor is called only once when the class or any of its derived is loaded in
clr before calling any instance constructor.
A public constructor is accessible anywhere in the application

A private constructor is restricted to the class within the application

Static constructor give access to the data without necessarily creating an object class


1 comments:

Satyam Soni said...

may create protected constructor and internal .if no then why? may i create class as protected if no then why ?and also method .