基本クラスと継承子クラスがあります。
基本クラスのコンストラクターは、子コンストラクターから渡したくないパラメーターを受け入れます。
パラメータはジェネリックです List<MyInterface>
それはC#で可能ですか?
現時点では、私の子クラスコンストラクターは次のようになっています。
public ChildClass() : base (new List<MyInterface>());
リストを新しくすることなく、より良い方法はありますか?
基本クラスのコンストラクターがパラメーターを予期している場合は、そのパラメーターを子クラスから渡す必要があります。
2つのオプションがあります。
protected
, as that will only be available to child classes)Like:
public class BaseClass
{
public BaseClass() // or protected
{
//does nothing
}
}
and then call it like:
public ChildClass() : base ();
or just
public ChildClass() //Since base() would be called implicitly.
null
as per your requirement.You should also revisit your design. Not having a parameterless constructor in the base class implies that the class object requires that particular parameter to to work. Working around it is a code smell.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加