このようなクラスがある場合:
public class Foo
{
public IEnumerable<Bar> Bars { get; set; }
public Foo()
{
Bars = new List<Bar>();
}
}
ある段階で、クラスをリファクタリングし、最初のコンストラクターを次のように実装するセカンダリ コンストラクターを追加します。
public class Foo
{
public IEnumerable<Bar> Bars { get; set; }
// some more properties were added
public Foo()
{
Bars = new List<Bar>();
}
public Foo(string parameter): this()
{
.... some code here
}
}
次のように書くこともできます。
public class Foo
{
public IEnumerable<Bar> Bars { get; set; }
// some more properties were added too
public Foo()
{
InitilizeFoo();
}
public Foo(string parameter)
{
InitilizeFoo();
.... some code here
}
private void InitializeFoo()
{
Bars = new List<Bar>();
}
}
このシナリオで両方のアプローチが機能するのを見て、一方を他方よりも使用する利点または欠点はありますか?
コンストラクターを継承する方が効率的で、そのコードをより高速に実行できますか、それとも 2 番目の実装をより効率的にすることについて私が知らない欠点がありますか?