zoukankan      html  css  js  c++  java
  • 编写高质量代码改善C#程序的157个建议——建议44:理解委托中的协变

    建议44:理解委托中的协变

    委托中的泛型变量天然是部分支持协变的。为什么是“部分支持协变”?看下面示例:

       class Program
        {
            public delegate T GetEmployeeHanlder<T>(string name);
    
            static void Main()
            {
                GetEmployeeHanlder<Employee> getAEmployee = GetAManager;
                Employee e = getAEmployee("Mike");
            }
    
            static Manager GetAManager(string name)
            {
                Console.WriteLine("我是经理: " + name);
                return new Manager() { Name = name };
            }
    
            static Employee GetAEmployee(string name)
            {
                Console.WriteLine("我是雇员: " + name);
                return new Employee() { Name = name };
            }
    
        }
    
        interface ISalary<T>
        {
            void Pay();
        }
    
        class BaseSalaryCounter<T> : ISalary<T>
        {
            public void Pay()
            {
                Console.WriteLine("Pay base salary");
            }
        }
    
        class Employee
        {
            public string Name { get; set; }
        }
        class Programmer : Employee
        {
        }
        class Manager : Employee
        {
        }

    上中的GetAManager返回的是一个Manager,但是在使用中,其实是将其赋值给一个泛型参数为Employee的委托变量。因为存在下面一种情况,所以编译不过:

                GetEmployeeHanlder<Manager> getAManager = GetAManager;
                GetEmployeeHanlder<Employee> getAEmployee = GetAManager;

    要让上面的代码编译通过,同样需要为委托中的泛型参数指定out关键字:

    public delegate T GetEmployeeHanlder<out T>(string name);

    除非考虑到该委托声明肯定不会用于可变性,否则,为委托中的泛型参数指定out关键字将会拓展委托的应用,建议在实际编码过程中永远这样使用。实际上,FCL4.0中的一些委托声明已经用out关键字来让委托支持协变了,如我们常常会使用到的:

    public delegate TResult Func<out TResult>();
    
    public delegate TOutput Converter<in TInput, out TOutput>(TInput input);

    转自:《编写高质量代码改善C#程序的157个建议》陆敏技

  • 相关阅读:
    fpga配置方式 .jic固化为ps模式
    fpga新建nios
    四轴飞行器飞行原理与双闭环PID控制
    fpga为什么要用nios 开发
    error A space is required after ',' comma-spacing
    vuex : Newline required at end of file but not found eol-last
    vue -Missing space before value for key 'path'vue.js解决空格报错
    visual studio 自动补全功能 以及代码没有颜色
    hadoop 伪分布模式环境搭建
    django框架-DRF工程之认证功能
  • 原文地址:https://www.cnblogs.com/jesselzj/p/4733207.html
Copyright © 2011-2022 走看看