Private Constructor in C#

Certifiedkiller2000
Posted by in C# category on for Beginner level | Points: 250 | Views : 13037 red flag
Rating: 4 out of 5  
 2 vote(s)

In this article we will learn creating a private contructor and instantiating an object.


 Download source code for Private Constructor in C#

Introduction


From the first time we started learning OOPs we have learnt about constructors that they are always publc with no return type self executable and same name as class name, but constructors can be private !! means we cannot instantiate any object of that class so wat is the use? suppose i know the name of the class and u have made a default public constructor there by i can create an object and can use the members of the class in order to prevent object creation i will made the constructor private. So the question is how to make an object.
We can create an object within the static method and then can create objects of the class , until and unless u dont know the method name we cant create objects of the class.

We create a static method with return type class which will return ur object

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace privatecontructor
{
    public class Class1
    {
        //private constructor
        private Class1()
        {
            
        }
        //create a static method with class return type, as it will return the object
        public static Class1 clobj()
        {
            Class1 h = new Class1();
            return h;
        }
        public void abc()
        {
            MessageBox.Show("hello");
        }
    }
}

Here we create a class type variable the keep the reference of the returned object

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace testprvconts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           // to store the object
            privatecontructor.Class1 a;

            a = privatecontructor.Class1.clobj();
            a.abc();
   
        }
    }
}



It will print hello!!
Rest is self explanatory. If you have any queries u r welcome . i am new so spare if there is any mistakes

Thus we created a object with a private constructor..


Page copy protected against web site content infringement by Copyscape

About the Author

Certifiedkiller2000
Full Name: Krishnendu Sinha
Member Level: Starter
Member Status: Member
Member Since: 7/16/2011 6:13:28 PM
Country: India
Don't worry about what people think, they don't do it very often.
http://www.dotnetfunda.com
Don't worry about what people think, they don't do it very often.

Login to vote for this post.

Comments or Responses

Posted by: Anup1252000 on: 7/17/2011 | Points: 25
Article whatever you provide is not very explanatory. I highly recommend you to read this article to understand why we need to use Private constructor

http://msdn.microsoft.com/en-us/library/ff650316.aspx


Posted by: Certifiedkiller2000 on: 7/18/2011 | Points: 25
@anup : Thanks i agree , I know nothing about design patterns! There can be many use depending upon how we implement. this was just a trival trial of mine. I just tried to create a private constutor and a object of the class the way i thought it can be created i used:)

Login to post response

Comment using Facebook(Author doesn't get notification)