泛型(带点委托)

泛型类:

    //泛型类
    class MyClass<T>
    {

        private T _val;

        public T Val
        {
            get { return _val; }
            set { _val = value; }
        }

        public MyClass(T[] _names)
        {
            this.names = _names;
        }

        private T[] names = null;


        public void Show()
        {
            foreach (T item in names)
            {
                Console.WriteLine(item);
            }
        }
    }
            MyClass mc = new MyClass(new string[] { "A", "B", "C" });
            MyClass<int> mc = new MyClass<int>(new int[] { 10, 20, 30 });
            MyClass<string> mc = new MyClass<string>(new string[] { "A", "B", "C" });
            mc.Show();
            Console.Read();

泛型方法:

    //泛型方法
    public class Person
    {
        //方法后直接写<T>就表示是一个泛型方法
        public void Show<T>(T msg)
        {
            Console.WriteLine(msg);
        }
    }

使用:

            Person p = new Person();
            //p.Show<string>("aaaaaa");

            //p.Show<int>(123);
            //对于泛型方法下面的两种调用方式都是正确的。没写的时候会自动识别
            p.Show("aaaaaa");
            p.Show<string>("fdsfds");

            Console.Read();

泛型接口:

    //泛型接口
    public interface IFlyable<T>
    {
        void Fly(T msg);

        T GetTValue(T msg);
    }

实现:

    //编写一个类来实现泛型接口
    //一个普通类实现了泛型接口
    public class SupperMan : IFlyable<string>
    {

        #region IFlyable<string> 成员

        public void Fly(string msg)
        {
            Console.WriteLine(msg);
        }

        #endregion
    }

    //泛型类实现泛型接口,尖括号内可以不是T,单多数定义为T,
//泛型类实现泛型接口,当泛型类是什么类型,就把该类型给泛型接口
//where 是限制条件
public class SpiderMan<YZK, W> : IFlyable<YZK> where YZK : new() { #region IFlyable<YZK> 成员 public void Fly(YZK msg) { throw new NotImplementedException(); } #endregion #region IFlyable<YZK> 成员 public YZK GetTValue(W msg) { return new YZK(); } #endregion }

 泛型委托:

注意,以后只要用到委托,基本上不用我们自己定义,.NET都已经给我们定义好了

public delegate void M1Delegate(string msg);

//泛型委托
public delegate void MGenericDelegate<T>(T arg);

#region 泛型委托
            //M1Delegate md = M1;
            //md("aaaaaaaa");
            //Console.Read();

            //M1Delegate md = M2;

            //Console.Read();
            //MGenericDelegate<string> md = M1;
            //md("aaaa");

            //MGenericDelegate<int> md = M2;
            //md(1000);

            MGenericDelegate<double> md = M3;
            md(993.5);
            Console.Read();






static void M1(string msg)
        {
            Console.WriteLine(msg);
        }
        static void M2(int msg)
        {
            Console.WriteLine(msg);
        }
        static void M3(double msg)
        {
            Console.WriteLine(msg);
        }
        static void M4(float msg)
        {
            Console.WriteLine(msg);
        }
        static void M5(Person msg)
        {
            Console.WriteLine(msg);
        }

系统内置泛型委托:

Func,无论多少泛型,永远最后一个泛型是返回值类型

Action没有返回值,泛型都是参数

list.where() 括号内是一个泛型委托

#region 系统内置的泛型委托
            //只要是Action委托都是无返回值的。一或以上个参数用泛型Action,无参无返回值用Action

            //1.存储无参数无返回值的方法
            Action md = () => { Console.WriteLine("无参数无返回值。"); };
            md();
            Console.Read();


            //2.有两个参数没有返回值
            Action<string, int> md = (s, i) => { Console.WriteLine(s + "   " + i); };
            md("aaaaaaa", 10);
            Console.Read();



            //当需要存储带返回值的方法的时候,就需要使用另外一个泛型委托Func

            //返回值是string
            Func<string> fn = T1;

            string ss = fn();
            Console.WriteLine(ss);
            Console.Read();


            //返回值是string类型,参数是一个int类型
            Func<int, string> fn = n => n.ToString();
            Console.WriteLine(fn(10));
            Console.Read();

            Func<int, int, string> fn = T2;
            Console.WriteLine(fn(12, 5));
            Console.Read();

            Action<string>
            #endregion

 泛型约束:

    //使用泛型约束,约束了T只能是值类型
    class MyClass<T> where T : struct
    {

    }


    //使用泛型约束,约束了T只能是引用类型不能是值类型
    class MyClass<T> where T : class
    {

    }

    //限制T必须是实现了某个接口的类型,要求T必须是实现了IComparable接口的子类型对象或者就是该接口类型对象 
    class MyClass<T> where T : IComparable
    {
    }


    //要求T必须是Person类型,或者是Person类的子类
    class MyClass<T> where T : Person
    {
    }

    //要求T必须是Person类型,或者是Person类的子类
    class MyClass<T, V>
        where T : Person
        where T : new() //要求将来传递进来的类型必须具有一个无参数的构造函数
        where V : T
    {
    }


    //对T没有要求,但是V必须是T类型或者T类型的子类型
    class MyClass<T, V>
        where V : T
    {
    }
原文地址:https://www.cnblogs.com/xiaoshi657/p/4668689.html