Exact code I'm trying to build:
public interface IMapContainer<out T> where T : MapRoomBase
{
String GetName();
IEnumerable<T> GetRooms();
}
I'm getting this error: Invalid variance: The type parameter 'T' must be invariantly valid on 'MapLibrary.IMapContainer.GetRooms()'. 'T' is covariant.
I was under the impression that this would be valid since IEnumerable simply returns the items, and none can be added. Why is this not safe + valid?
Make sure you're not targeting an old framework version. IEnumerable<T>
is covariant starting with .NET 4. Your code compiles fine under .NET 4 and fails with the error you mention on .NET 3.5.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With