c# - 为什么 .NET 4.0 协方差在此示例中不起作用?

标签 c# .net-4.0 covariance

我在这些行上遇到编译器错误:

            RenderLookup(Cars);
            RenderLookup(Employees);


Error   2   Argument 1: cannot convert from 'Demo.MyList<Demo.Car>' to 'System.Collections.Generic.IEnumerable<Demo.KeyedBase>' Program.cs  85  26  Demo

Error   4   Argument 1: cannot convert from 'Demo.MyList<Demo.Employee>' to 'System.Collections.Generic.IEnumerable<Demo.KeyedBase>'    Program.cs  86  26  Demo

这是怎么回事?我以为 .NET 4.0 会处理这个问题?我错过了什么?

using System;
using System.Collections.Generic;

namespace Demo
{
    public interface KeyedBase
    {
        int Key { get; }
        string Description { get; }
    }

    public class MyList<T> : Dictionary<int, T> where T : KeyedBase
    {
        public void Add(T itm)
        {
            Add(itm.Key, itm);
        }
    }

    public class Car : KeyedBase
    {
        private readonly int _ID;
        private readonly string _Description;
        public Car(int ID, string Description)
        {
            _ID = ID;
            _Description = Description;
        }

        public int Key
        {
            get { return _ID; }
        }

        public string Description
        {
            get { return _Description; }
        }
    }

    public class Employee : KeyedBase
    {
        private readonly int _ID;
        private readonly string _FirstName;
        private readonly string _LastName;
        public Employee(int ID, string FirstName, string LastName)
        {
            _ID = ID;
            _FirstName = FirstName;
            _LastName = LastName;
        }

        public int Key
        {
            get { return _ID; }
        }

        public string Description
        {
            get { return _LastName + ", " + _FirstName; }
        }
    }

    class Program
    {
        private static void RenderLookup(IEnumerable<KeyedBase> Lookup)
        {
            Console.WriteLine("Choose:");
            foreach (var itm in Lookup)
            {
                Console.WriteLine("{0} : {1}", itm.Key, itm.Description);
            }
        }

        static void Main(string[] args)
        {
            var Cars = new MyList<Car> { new Car(1, "Subaru"), new Car(2, "Volswagen") };

            var Employees = new MyList<Employee>
                                {
                                    new Employee(1, "Mickey", "Mouse"),
                                    new Employee(2, "Minnie", "Mouse")
                                };

            RenderLookup(Cars);
            RenderLookup(Employees);
        }
    }
}

最佳答案

您正在尝试转换 Dictionary<int, T> -派生类到IEnumerable<T> .您必须将其转换为 IEnumerable<KeyValuePair<int, T>>或通过 Values从你的字典中收集。

RenderLookup(Cars.Values);
RenderLookup(Employees.Values);

这不是协方差的问题,而是类型兼容性的问题。

关于c# - 为什么 .NET 4.0 协方差在此示例中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5614479/

相关文章:

用于解析 JSON 结果的 C# 类

c# - 需要具有 .NET 4.0 新功能的单一生产者/单一消费者模式示例

c# - 如何在 C# 中使用通用接口(interface)从子对象引用父对象?

c# - 不同固件中的协方差导致代码中断?

C# TcpClient.Connect 通过代理

javascript - 由于语法无效,服务器无法处理 PUT 请求 (ASP.NET)

c# - 在 C# 中截取特定区域的屏幕截图?

c# - 带左自连接的 NHibernate QueryOver

C++11 std::function 不接受多态参数

C++ 模板、多态性和模板协变