在C#4中,如何在具有重载构造函数的父类的子类中具有带可选参数的构造函数?

cde*_*zaq 5 c# inheritance constructor subclass constructor-chaining

我有一个父类,它有一个重载的构造函数,我有一个子类,它有一个带有可选参数的构造函数.有没有办法让子类的构造函数仍然暴露父类的重载,同时保留它自己的可选参数?

这是两个类及其所需构造函数的一些示例代码:

class Foo {
    Foo(String arg0) 
    {
      // do some stuff with arg0
    }

    Foo(String arg0, List<x> arg1)
        : this(arg0)
    {
      // do some other stuff with arg1 that is special because we have an arg1
    }
}

class Bar : Foo {
    Bar(String arg0, List<y> arg2 = null, String arg3 = "") 
        : base(arg0)
    {
      // some third thing with arg2 and arg3
    }
}
Run Code Online (Sandbox Code Playgroud)

这是其他子类构造函数的方法签名我也想要公开父构造函数的重载,但问题是如何做到这一点:

Bar(String arg0, List<x> arg1, List<y> arg2 = null, String arg3 = "")
Run Code Online (Sandbox Code Playgroud)

我认为,我找到了一个解决方案,但我不确定它是否尽可能干净.我发布它作为答案,以防它是唯一的选择.

cde*_*zaq 2

这是我想出的解决方案:

class Foo {
    Foo(String arg0) 
    {
      // do some stuff with arg0
    }

    Foo(String arg0, List<x> arg1)
        : this(arg0)
    {
      // do some other stuff with arg1
    }
}

class Bar : Foo {
    Bar(String arg0, List<y> arg2 = null, String arg3 = "") 
        : base(arg0)
    {
        this.Initialize( arg2, arg3);
    }

    Bar(String arg0, List<x> arg1, List<y> arg2 = null, String arg3 = "")
        : base(arg0, arg1)
    {
      this.Initialize( arg2, arg3);
    }

    private void Initialize(List<y> arg2, String arg3)
    {
      // some third thing with arg2 and arg3
    }
}
Run Code Online (Sandbox Code Playgroud)

这似乎有点不干净,因为我没有将子类的构造函数链接在一起,而是调用一个函数,但我想不出任何其他方法来做到这一点。