协变(List泛型作为方法参数时的父类子类问题)

有段时间没搞.net的项目了(没办法,谁让国内JAVA流行是事实)。最近又回归.net(哪里需要哪里搬~)。

接收到需求后,一顿输出,结果...咦?编译失败???

错误信息:

csharp 复制代码
CS1503:参数1:无法从"System.Collections.Generic.List<Student>" 转换为"Person"

额...仔细回想下(好久不搞.net,.net的理论知识都已经落灰了)。

下面是完整的测试代码:

csharp 复制代码
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}
public class Student :Person
{
    public string School { get; set; }
    public decimal Score { get; set; }
}
public abstract class Point
{ 
    public Person person { get; set; }
    public List<Person> persons { get; set; }
    /// <summary>
    /// 单个
    /// </summary>
    public abstract void Info();

    /// <summary>
    /// 批量
    /// </summary>
    public abstract void List();
}

public class PointStudent : Point
{
    public PointStudent(Person data)
    {
        this.person = data;
    }
    public PointStudent(List<Person> data)
    {
        this.persons = data;
    }
    public override void Info()
    {
        Console.WriteLine($"{((Student)person).Name}就读于{((Student)person).School}");
    }
    public override void List()
    {
        this.persons.ForEach(p => { this.person = p; Info(); });
    }
}

static void Main(string[] args)
{
    Student student = new Student() { Name="张三",Age = 18,School ="东师"};
    Point point = new PointStudent(student);
    point.Info();

    List<Student> students = new List<Student>() { student };
    Point pointList = new PointStudent(students);
    pointList.List();
}

那么问题来了...

为什么使用子类的对象作为参数传入时没有错误?

答:

  • 后期绑定(Polymorphism):C# 支持后期绑定,这意味着在运行时确定方法的实际执行版本,而不是在编译时确定。

  • 继承和多态性:子类继承了基类的所有特性,同时可以拥有自己的特性和行为。当你将子类对象传递给期望基类类型的方法时,方法将根据子类对象的实际类型执行相应的操作。

为什么使用List时报错?

答:

  • 对于List和List,这是List类型,可以理解为2个不相干的类,不存在继承关系,所以不能直接作为参数传入。

怎么解决?

答:

  • 这里就用到了协变, 协变允许将一个派生类对象赋值给一个基类对象。

协变示例:

csharp 复制代码
// Covariance.
IEnumerable<string> strings = new List<string>();  
// An object that is instantiated with a more derived type argument
// is assigned to an object instantiated with a less derived type argument.
// Assignment compatibility is preserved.
IEnumerable<object> objects = strings;

从C#4.0开始,IEnumerable 的声明中引入了 out 关键字,它表示这个接口支持协变性。

csharp 复制代码
[TypeDependency("System.SZArrayHelper")]
[__DynamicallyInvokable]
public interface IEnumerable<out T> : IEnumerable
{
    [__DynamicallyInvokable]
    new IEnumerator<T> GetEnumerator();
}

那么上面这段代码该怎么修改?

csharp 复制代码
public class Person
{
        public string Name { get; set; }
    public int Age { get; set; }
}

public class Student :Person
{
        public string School { get; set; }
    public decimal Score { get; set; }
}

public abstract class Point
{ 
    public Person person { get; set; }

    public IEnumerable<Person> persons { get; set; }
    /// <summary>
    /// 单个
    /// </summary>
        public abstract void Info();
    /// <summary>
    /// 批量
    /// </summary>
    public abstract void List();
}

public class PointStudent : Point
{
    public PointStudent(Person data)
    {
        this.person = data;
        }
    public PointStudent(IEnumerable<Person> data)
    {
        this.persons = data;
        }
    public override void Info()
    {
        Console.WriteLine($"{((Student)person).Name}就读于{((Student)person).School}");
        }
    public override void List()
    {
        this.persons.ForEach(p => { this.person = p; Info(); });
    }
}

static void Main(string[] args)
{
    Student student = new Student() { Name="张三",Age = 18,School ="东师"};
    Point point = new PointStudent(student);
    point.Info();

    List<Student> students = new List<Student>() { student };
    Point pointList = new PointStudent(students);
    pointList.List();
}

其实代码改动比较简单,大多数的程序员都知道该怎么去调整,这里我就多啰嗦了,需要注意的是:在岗位上拧螺丝的时候,不要忘了去回顾下自己的知识栈,保持技术的鲜活~


相关推荐
hqwest3 小时前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
orangapple3 小时前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf
追逐时光者14 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 50 期(2025年8.11-8.17)
后端·.net
★YUI★18 小时前
学习游戏制作记录(玩家掉落系统,删除物品功能和独特物品)8.17
java·学习·游戏·unity·c#
谷宇.18 小时前
【Unity3D实例-功能-拔枪】角色拔枪(二)分割上身和下身
游戏·unity·c#·游戏程序·unity3d·游戏开发·游戏编程
LZQqqqqo19 小时前
C# 中 ArrayList动态数组、List<T>列表与 Dictionary<T Key, T Value>字典的深度对比
windows·c#·list
Dm_dotnet21 小时前
Stylet启动机制详解:从Bootstrap到View显示
c#
三千道应用题1 天前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
唐青枫1 天前
别滥用 Task.Run:C# 异步并发实操指南
c#·.net
我好喜欢你~1 天前
C#---StopWatch类
开发语言·c#