Pages

Wednesday, 26 February 2014

Private Constructor in C# with Example

Private Constructor in C# with Example

using System;
namespace ConsoleApplication3
{
    public class Sample
    {
        public string param1, param2;
        public Sample(string a, string b)
        {
            param1 = a;
            param2 = b;
        }
        private Sample()  // Private Constructor Declaration
        {
            Console.WriteLine("Private Constructor with no prameters");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Here we don't have chance to create instace for private constructor
            Sample obj = new Sample("Welcome", "to http://fantasyaspnet.blogspot.in/");
            Console.WriteLine(obj.param1 + " " + obj.param2);
            Console.ReadLine();
        }
    }
}

Demo:

Welcome to http://fantasyaspnet.blogspot.in/

No comments:

Post a Comment