Derived List to Base IEnumerable

前端 未结 2 1557
失恋的感觉
失恋的感觉 2021-01-24 15:51

I have the following code that compiles in .NET Framework version 4.0 and above:

public abstract class MyBase { }
public class MyDerived : MyBase { }

public abs         


        
2条回答
  •  日久生厌
    2021-01-24 16:29

    In .Net 4.0 the IEnumerable interface was changed from:

    public interface IEnumerable

    To public interface IEnumerable

    Notice that the word out has been added to the generic type parameter. This means that the generic parameter is co-variant which means you can pass in a more derived type.

    Covariance Enables you to use a more derived type than originally specified. You can assign an instance of IEnumerable (IEnumerable(Of Derived) in Visual Basic) to a variable of type IEnumerable

    See msdn for more information

提交回复
热议问题