LoginSignup
1
2

More than 5 years have passed since last update.

C#のジェネリックの型引数の制限条件にインターフェースが使えない

Last updated at Posted at 2018-01-11

ジェネリックの型引数の制約条件にインターフェースを使ったら、

Inconsistent accessibility: constraint type 'ISavable' is less accessible than 'ReactiveSavableList' [Assembly-CSharp]

とのエラー。
コードは下記の通り

// ISavable
interface ISavable
{
  void Save();
}

// ReactiveSavableList
public class ReactiveSavableList<T> where T:ISavable
{
}

Interfaceはインスタンス化できない
→コンストラクタがprivate?
→ReactiveSavableListのpublicなコンストラクタとの間に、アクセシビリティのエラーが出ている?

と思ったので、下記の通りに修正したらエラー取れました。
以下、修正後のコード。

// ISavable
abstract public class ISavable
{
  public void Save()
  {
  }
}

// ReactiveSavableList
public class ReactiveSavableList<T> where T:ISavable
{
}

命名はAbstract◯◯みたいに変えると良さそうです。

※追記(2018-01-11 21:06)
上のエラーが出ているコードでも、interfaceをpublicにすれば解決します。
YuneKichiさんありがとうございました!

1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2