0

While i tried to search for the use of Private Constructors, all i found was its use in Singleton pattern. Other than Singleton pattern what is the use Private Constructor. Some links or code examples would be appreciated.

iJade
  • 23,144
  • 56
  • 154
  • 243

3 Answers3

3

Here's two other reasons:

The Factory Pattern.

  • Uses private constructor(s) and public static methods to call them.

The Builder pattern for immutable classes.

  • Uses a nested mutable Builder class, an instance of which is passed to a private constructor.

Sample Builder pattern:

using System;

namespace Demo
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            var demo = new ImmutableClass.Builder{
                A = 1,
                B = "two",
                C = 3.0
            }.Build();
        }
    }

    public sealed class ImmutableClass
    {
        public sealed class Builder
        {
            public int A;
            public string B;
            public double C;

            public ImmutableClass Build()
            {
                return new ImmutableClass(this);
            }
        }

        private ImmutableClass(Builder builder)
        {
            _a = builder.A;
            _b = builder.B;
            _c = builder.C;
        }

        public int A
        {
            get
            {
                return _a;
            }
        }

        public string B
        {
            get
            {
                return _b;
            }
        }

        public double C
        {
            get
            {
                return _c;
            }
        }

        private readonly int _a;
        private readonly string _b;
        private readonly double _c;
    }
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
2

quick answer: serialization and chained constructors.

Longer answer :

Serialization of objects that requires a value for initialization

public class SomeSerializableClass {

    private SomeSerializableClass() {} // Used only in serialization

    public SomeSerializableClass(int initParameter){
        this.Property = initParameter;
    }

    public int Property { get; set; }

}


public class Program{

    static void Main(){
        var obj1 = new SomeSerializableClass(42); // valid
        var obj2 = new SomeSerializableClass(); // invalid

        var xs = new XmlSerializer(typeof(SomeSerializableClass));
        var obj3 = (SomeSerializableClass)xs.Deserialize(someStream); // possible

    }

}

Chained constructors (factorizing some constructor logic):

public class SomeClass {

    private SomeClass(int initParameter) {
        this.Property = initParameter; // Will always be executed, the logic exists once        
    }


    public SomeSerializableClass(int initParameter, string otherParameter)
        : this(initParameter)
    {
    }
    public SomeSerializableClass(int initParameter, int againAntherParameter)
        : this(initParameter)
    {
    }

    public int Property { get; set; }

}
Steve B
  • 36,818
  • 21
  • 101
  • 174
0

I use a private constructor when initializing an object from e.g. a DataReader off of the back of a static Search(...) function - such a constructor makes little sense if public.

Will A
  • 24,780
  • 5
  • 50
  • 61