Friday, 23 December 2016

private constructor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    class privateConstructor
    {
    //{
    //    Private constructor is a special instance constructor used in a class that contains static member only. If a class has one or more private constructor and no public constructor then other classes is not allowed to create instance of this class this mean we can neither create the object of the class nor it can be inherit by other class. The main purpose of creating private constructor is used to restrict the class from being instantiated when it contains every member as static.
        public static string st = string.Empty;
        public  string st1 = string.Empty;
        public string stw()
        {
            return "sttest";
        }
        public privateConstructor(string st1, string st2)
        {
            Console.WriteLine("Hi"+st1+"and hi"+st1);

        }
        private privateConstructor()
        {
            Console.WriteLine("This is the private cinstructor that can not instiated0");

        }

    }
    class program
    {
        public static void Main5(string[] ar)
        {
            privateConstructor objPrivatecontructor = new privateConstructor("hi p1","p2");
            objPrivatecontructor.st1 = "test";
        }
    }

}

No comments:

Post a Comment