zoukankan      html  css  js  c++  java
  • 泛型委托示例

    delegate T Factory<out R, in S , T>() 

    //   out R  协变        in  S   逆变     T   不变

    ----------------------------------------------------------------------------------------------------

    public delegate TR Func<T1, T2, TR>(T1 p1, T2 p2);   //泛型委托  TR委托返回类型   T1,T2 委托参数类型

    class Simple

    {

      static public string PrintString(int p1, int p2) //方法匹配委托

      {

        int total = p1 + p2;

        return total.ToString();

      }

    }

    class Program

    {

      static void Main()

        {

          var myDel = new Func <int, int, string>(Simple.PrintString);   //创建委托实例

          Console.WriteLine("ToTal: {0}", myDel(15, 13));//  调用委托

        }

    }

    另一种out协变

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication33
    {
        class Animal                                            //基类
        {
            public int Legs = 4;
        }

        class Dog:Animal                                        //派生类
        {

        }

        class Program
        {
            delegate T Factory<out T>();

            static Dog MackDog() { return new Dog(); }
            static void Main(string[] args)
            {
                Factory<Animal> animalMacker = MackDog;             //隐式强制转换

                Factory<Dog> dogMacker = MackDog;

                Factory<Animal> animalMacker2 = dogMacker;          //需要out标识符

                Factory<Animal> animalMacker3 = new Factory<Dog>(MackDog);  //需要out标识符
            }
        }
    }

  • 相关阅读:
    Navicat Premium12以上版本多用户破解方法
    Linux并行gzip压缩工具pigz
    Windows Server 2019远程桌面服务配置和授权激活
    mysql删除大表
    KVM qcow2 磁盘在线扩容方法
    在jenkins中连接kubernetes集群
    CentOS 7部署 Ceph分布式存储架构
    (转)关于T(n) = kT(n/c) + f(n) 的时间复杂度
    算法中的思想(第0篇)
    (求通俗易懂的证法) 过n个有标志顶点的树的数目等于n^(n-2)
  • 原文地址:https://www.cnblogs.com/bedfly/p/11921520.html
Copyright © 2011-2022 走看看