locked
Private Constructor RRS feed

  • Question

  • User-1786106048 posted

    In singlton pattarn , how , when the private constructor is called , pls give some practical example

    Friday, May 20, 2011 3:02 AM

Answers

  • User1288683547 posted

     singlton pattarn main use, one time object creation... so we use private construct to avoid to create many instance...

    for example

    public class xyz
    {
       private xyz() {}
       static readonly xyz myins = new xyz();
       public static xyz Instance 
                {
                    get
                      { return myins; }
    
                }
    
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 1, 2011 7:21 AM

All replies

  • User1872574491 posted

    Hi,

    The private constructor means the class can not insitated from other class.

    The private constructor can only be called from inside the class itself.

    Friday, May 20, 2011 8:59 AM
  • User1288683547 posted

     singlton pattarn main use, one time object creation... so we use private construct to avoid to create many instance...

    for example

    public class xyz
    {
       private xyz() {}
       static readonly xyz myins = new xyz();
       public static xyz Instance 
                {
                    get
                      { return myins; }
    
                }
    
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 1, 2011 7:21 AM