62,244
社区成员




using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Test1> list = new List<Test1>
{
new Test1 {t = "t1"},
new Test1 {t = "t2"}
};
IEnumerable<Base> strings = list;
foreach (var item in strings)
{
Console.WriteLine(item.b);
}
}
}
public class Base
{
public string b { get; set; }
}
public class Test1:Base
{
public string t { get; set; }
}
}
var
result = list.Cast(x=> (Base)x);
这样的转换代码使得 list 能转换为 Base 的可枚举列表。有了协变逆变的 in、out 声明,那么在过程接口就直接支持多态了,不需要自己写列表的转换语句了。