C# 泛型

泛型方法  ShowProp    对比    普通方法  ShowProp<T>

 public class GenericMethod
    {
        public static void Show<T>(T t)
        {
            Console.WriteLine($"传入参数是: {t},它的类型是 {t.GetType().Name}");
        }
        public static void ShowProp(object o)
        {
            Console.WriteLine($"传入参数是: {o},它的类型是 {o.GetType().Name}");
            // Console.WriteLine($"{o.Id}___{o.Name}");
            Console.WriteLine($"{((People)o).Id}___{((People)o).Name}");
        }
        public static  void  ShowProp2<T> (T t) where T : People
        {
            Console.WriteLine($"传入参数是: {t},它的类型是 {t.GetType().Name}");
            // Console.WriteLine($"{o.Id}___{o.Name}");
            Console.WriteLine($"{t.Id}_______{t.Name}");
        }


    }



   public class People
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public void Hi()
        {
            Console.WriteLine("Hi ");
        }
    }

泛型约束  Where T:

  public class GenericConstraint
    {
  

        public static  T Get<T>(T t) where T: ISports  //类型参数必须是指定的接口或是指定接口的实现。可以指定多个接口约束。接口约束也可以是泛型的。
        {
            return t;
        }

        public static T Get2<T>(T t) where T: class  //类型参数必须为类型。
        {
            return t;
        }

        public static T Get3<T>(T t) where T : struct   //类型参数必须为值类型
        {
            return t;
        }
        public static T Get4<T>(T t) where T : new() //类型参数必须有一个公有、无参的构造函数。当于其它约束联合使用时,new()约束必须放在最后。
        {
            return t;
        }
        public static T Get5<T>(T t) where T : People//类型参数必须是指定的基类型或是派生自指定的基类型。
        {
            return t;
        }
        public static T Get6<T>(T t) where T : People, ISports, IWork, new()
        {
            return t;
        }
    }

泛型类、泛型接口、泛型委托

   public class GenericClass<T>//泛型类
    {
        public T _T;
    }

    interface IGenericInterface<T>//泛型接口
    {
        T GetT(T t);
    }

    public delegate void SayHi<T>(T t);//泛型委托

协变、逆变

 class GenericExtend
    {
        ICustomListOut<Bird> customListOut = new CustomListOut<Sparrow>();//协变
        IEnumerable<Bird> birdList = new List<Sparrow>();//协变
        Func<Bird> func = new Func<Sparrow>(() => null);//协变
        ICustomListIn<Sparrow> customListIn = new CustomListIn<Bird>();//逆变
        Action<Sparrow> act = new Action<Bird>((Bird b)=> { });//逆变
    }

  public interface ICustomListIn<in T>
    {
        void Show(T t);
    }

public class CustomListIn <T>: ICustomListIn< T>
    {
       public  void Show(T t)
        {
           
        }

    }

    public interface ICustomListOut<out T>
    {
        T Get();
    }

 public class CustomListOut<T> : ICustomListOut<T>
    {
        public T Get()
        {
            return default(T);
        }
    }


    public class Bird
    {
        public int Id { get; set; }

    }
    public class Sparrow: Bird
   {
        public string Name { get; set; }
    }

协变、逆变 示例

 class GenericExtend2
    {
        IMyList<Bird, Sparrow> myList1 = new MyList<Bird, Sparrow>();
        IMyList<Sparrow, Sparrow> myList2 = new MyList<Bird, Sparrow>(); //逆变   (传入类型)
        IMyList<Sparrow, Bird> myList3 = new MyList<Bird, Sparrow>();// 逆变(传入类型)+协变(返回类型)
    }
   
    public interface IMyList<in T,out U>
    {
        void Show<T>(T t);
        U Get();
        U ShowGet(T t);
    }


    public class MyList<T, U> : IMyList<T, U>
    {
        public U Get()
        {
            return default(U);
        }

        public void Show<T1>(T1 t)
        {
            Console.WriteLine($"{t.GetType().Name}");
        }

        public U ShowGet(T t)
        {
            Console.WriteLine($"{t.GetType().Name}");
            return default(U);
        }
    }

泛型缓存

  public class GenericCache<T>
    {
        private static string _ReturnString;
        static GenericCache()
        {
            _ReturnString = DateTime.Now.ToString();//DateTime.Now.ToString(); 模拟存储缓存值
        }
        public static string GetCache()
        {
            return _ReturnString;
        }
       
    }

泛型知识介绍:   https://www.cnblogs.com/cplvfx/articles/12167884.html

原文地址:https://www.cnblogs.com/for-easy-fast/p/12311924.html