Why am I getting “The type parameter must be invariantly valid…” error?

前端 未结 2 1021
庸人自扰
庸人自扰 2020-12-19 05:40

I\'ll attempt to shorten this code example:

public interface IThing
{
    //...  Stuff
}

public class Thing1 : IThing
{  
}

public class Thing2 : IThing
{          


        
相关标签:
2条回答
  • 2020-12-19 06:12

    You cannot make a covariant ICollection<T>, since it allows you to put Ts into it.

    You can make a covariant read-only collection, a contravariant write-only collection, or an invariant read-write collection.
    You can't do both, or it wouldn't be typesafe.

    0 讨论(0)
  • 2020-12-19 06:30

    To expand on SLaks answer:
    To make your code compile, change the return type of ViewAll from ICollection<T> to IEnumerable<T>.

    0 讨论(0)
提交回复
热议问题