user339160
user339160

Reputation:

Constructor Chaining in C#

I am trying to chain these constructors:

public class Method1
{
    public Method1(string a, string b) : this(a, b, "", "")
    {

    }

    public Method1(string c, string d) : this("", "", c, d)
    {

    }

    public Method1(string a, string b, string c, string d)
    {

    }
} 

Is there any way I can achieve this?
Currently it's showing a compile time error.

Upvotes: 0

Views: 1486

Answers (5)

Bob Lokerse
Bob Lokerse

Reputation: 453

Depending of what kind the variables a, b, c and d represent, and how you use them, you could make structs for them. Then you can create overloads for the constructors, that would make it easier to comprehend.

using System;

namespace ConstructorChaining
{
    class Program
    {
        static void Main(string[] args)
        {
            var chainerAB = new ConstructorChainer(new A("aa"), new B("bb"));
            Console.WriteLine($"chainerAB - A:{chainerAB.A.Value}, B:{chainerAB.B.Value}, C:{chainerAB.C.Value}, D:{chainerAB.D.Value}");
    
            var chainerCD = new ConstructorChainer(new C("cc"), new D("dd"));
            Console.WriteLine($"chainerCD - A:{chainerCD.A.Value}, B:{chainerCD.B.Value}, C:{chainerCD.C.Value}, D:{chainerCD.D.Value}");
        }
    }


    public class ConstructorChainer
    {
        public A A { get; set; }
        public B B { get; set; }
        public C C { get; set; }
        public D D { get; set; }


        public ConstructorChainer(A a, B b) : this(a, b, new C(""), new D("")){}
    
        public ConstructorChainer(C c, D d) : this(new A(""),  new B(""), c, d){}

        private ConstructorChainer(A a, B b, C c, D d)
        {
            A = a;
            B = b;
            C = c;
            D = d;
        }
    }

    public struct A
    {
        public A(string value)
        {
            Value = value;
        }

        public string Value{ get; set;}
    }

    public struct B
    {
        public B(string value)
        {
            Value = value;
        }

        public string Value{ get; set;}
    }

    public struct C
    {
        public C(string value)
        {
            Value = value;
        }

        public string Value{ get; set;}
    }

    public struct D
    {
        public D(string value)
        {
            Value = value;
        }

        public string Value{ get; set;}
    }
}

Upvotes: -1

toadflakz
toadflakz

Reputation: 7934

You're using C# 4, so why not use optional parameters?

 public Method1(string a = "", string b = "", string c = "", string d = "")

You can call your desired behaviour as:

Method1(c: "some value", d: "some other value");

Method1(a: "some other other value", b:"another other value");

...or any combination of the parameters.

Upvotes: 5

Matthew Watson
Matthew Watson

Reputation: 109792

The common approach to this kind of thing is to have a private constructor that has all the parameters you need, and then to write a set of well-named public static methods that call the private constructor with the appropriate parameters.

This is a nice approach because you can give sensible names to your static creator methods, whereas with constructors you can't (of course).

For example:

public class Method1
{
    public static Method1 Method1FromAB(string a, string b)
    {
        return new Method1(a, b, "", "");
    }

    public static Method1 Method1FromCD(string c, string d)
    {
        return new Method1("", "", c, d);
    }

    private Method1(string a, string b, string c, string d)
    {

    }
}

Upvotes: 4

mybirthname
mybirthname

Reputation: 18127

public Method1(string a, string b) : this(a, b, "", "")
{

}

public static Method1 CreateMethodWithEndParams(string c, string d)
{
    Method1 method = new Method1("", "", c, d);

    return method;
}

public Method1(string a, string b, string c, string d)
{

}

You can have something like this. When you want to create the object with c,d

 Method1 method = Method1.CreateMethodWithEndParams("c", "d");

Upvotes: 1

Kerdan
Kerdan

Reputation: 133

public Method1(string a, string b) : this(a, b, "", "")

and

public Method1(string c, string d) : this("", "", c, d)

have the same method signature

you can perform this but not with the same method signature for your constructors

Upvotes: 2

Related Questions