Let’s get started from example. We have class Document and we want to initialize object of this class using two cases. At first we can say, that our new document will have concrete number. In second case, we want to create document with some content, but without number (number will be created automatically).

    public class Document
    {
        string num;
        string content;
        public Document(string num)
        {
            this.num = num;
        }
        public Document(string content)
        {
            this.content = content;
        }
    }

C# can’t compile this code, because we should have only one constructor having same signature.

In this case we can use Factory method to create document object using different methods. Let’s see new example.

    public class Document
    {
        private string num;
        private string content;
        public static Document CreateWithNum(string num)
        {
            Document document = new Document();
            document.num = num;
            return document;
        }
        public static Document CreateWithContent(string content)
        {
            Document document = new Document();
            document.content = content;
            return document;
        }
    }

And we can use new class very easy.

    static void Main(string[] args)
    {
        Document document1 = Document.CreateWithNum("P-1");
        Document document2 = Document.CreateWithContent("Some text");
    }