天天看点

【C#】【Generic】泛型复习笔记

什么是泛型,泛型有什么作用?

  • 泛化的类型 , 与具体的相对 。
  • 将具体的变为一般的 ,根据需求进行特化。
  • 避免了类的膨胀、 成员膨胀的问题。
using System;


namespace HelloGeneric
{
    class Program
    {
        static void Main(string[] args)
        {

            //类型膨胀 
            Apple apple = new Apple() { Color = "Red" };
            AppleBox box = new AppleBox() { Cargo = apple };
            Console.WriteLine(box.Cargo.Color);

            Book book = new Book(){Name = "New book"};
            BookBox bookBox = new BookBox() { Cargo = book };
            Console.WriteLine(bookBox.Cargo.Name);

            Box<Apple> box1 = new Box<Apple>() { Cargo = apple };
            Box<Book> box2 = new Box<Book>() { Cargo = book };

            Console.WriteLine(box1.Cargo.Color);
            Console.WriteLine(box2.Cargo.Name);


        }
    }

    class Book
    {
        public string Name { get; set; }
    }

    class Apple
    {
        public string Color { get; set; }
    }

    class AppleBox
    {
        public Apple Cargo { get; set; }
    }

    class BookBox
    {
        public Book Cargo { get; set; }
    }

    //类型参数 标识符 代表一个泛化的类型
    class Box<TCargo>
    {
        public TCargo Cargo { get; set; }
    }
}

           

泛型接口

  • 泛型类
  • 实现特化的泛型接口类
using System;


namespace HelloGeneric2
{
    class Program
    {
        static void Main(string[] args)
        {
            Student<int> stu = new Student<int>();
            stu.ID = 101;
            stu.Name = "Sai";
        }
    }

    interface IUnique<TId>
    {
        TId ID{get;set;}
    }
	
	//泛型类
    class Student<TId> : IUnique<TId>
    {
        public TId ID { get; set; }
        public string Name { get; set; }
    }
	
	//实现特化的泛型接口
    class Student : IUnique<ulong>
    {
        public ulong ID { get; set; }
        public string Name { get; set; }
    }
}

           

常用的泛型接口、泛型类

  • List 动态数组
  • Dictionary 字典
//List 动态数组
            IList<int> list = new List<int>();
            for (int i = 0; i < 100; i++)
            {
                list.Add(i);
            }

            foreach (var item in list)
            {
                Console.WriteLine(item);
            }

            //Dictionary 字典
            IDictionary<int, string> dict = new Dictionary<int, string>();
            dict[1] = "Sai";
            dict[2] = "Michael";
            Console.WriteLine($"Student #1 is {dict[1]} ,Student #2 is {dict[2]} ");
           

泛型方法

在调用的时候, 类型参数可以自动推断。不需要显式的写特化类型。

using System;
using System.Collections.Generic;

namespace HelloGeneric3
{
    class Program
    {
        static void Main(string[] args)
        {
        	//自动推断
            var arr = Add(1.1, 2.2);      
            foreach (var item in arr)
            {
                Console.WriteLine(item);
            }
        }

        static int[] Add(int a, int b)
        {
            int[] zip = new int[2];
            zip[0] = a;
            zip[1] = b;
            return zip;
        }

        static T[] Add<T>(T a, T b)
        {
            T[] zip = new T[2];
            zip[0] = a;
            zip[1] = b;
            return zip;
        }
    }
}

           

泛型方法和重载

顺序 : 有重载优先调用

using System;
using System.Collections.Generic;

namespace HelloGeneric3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("====== 泛型方法 ======");
            //var arr = Add(1.1, 2.2);  优先匹配对应的非泛型方法
            var arr = Add<double>(1.1, 2.2); //泛型方法
          
            foreach (var item in arr)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();
        }

        static int[] Add(int a, int b)
        {
            int[] zip = new int[2];
            zip[0] = a;
            zip[1] = b;
            return zip;
        }

        static double[] Add(double a , double b)
        {
            double[] zip = new double[1];
            zip[0] = a + b;
            return zip;
        }

        static T[] Add<T>(T a, T b)
        {
            T[] zip = new T[2];
            zip[0] = a;
            zip[1] = b;
            return zip;
        }
    }
}

           

泛型委托

Action

类型参数格式 : < T > <T1,T2> <T1,T2,T3> …

类型参数表示 : 此委托封装的方法的参数类型。可以多个参数。

Action委托只能引用没有返回值的方法。

using System;

namespace HelloGeneric4
{
    class Program
    {
        static void Main(string[] args)
        {
            Action<string> a1 = Say;
            a1.Invoke("Sai");
            //a1("Sai");
            Action<int> a2 = Mul;
            a2(1);
        }

        static void Say(string str)
        {
            Console.WriteLine($"Hello {str}!");
        }

        static void Mul(int x)
        {
            Console.WriteLine(x * 100);
        }
    }
}

           

Func

类型参数格式 : <T,TResult> <T1,T2,TResult> <T1,T2,T3,TResult> …

类型参数表示 : 此委托封装的方法的 参数的类型 和 返回值的类型。 可以多个参数。

using System;

namespace HelloGeneric4
{
    class Program
    {
        static void Main(string[] args)
        {
            // 会根据委托的类型参数 自动匹配对应方法
            Func<double, double, double> func1 = Add;
            var result = func1(100, 200);
            Console.WriteLine(result);
        }

        static int Add(int a, int b)
        {
            return a + b;
        }

        static double Add(double a,double b)
        {
            return a + b;
        }
    }
}

           

泛型与 lambda 表达式

using System;

namespace HelloGeneric4
{
    class Program
    {
        static void Main(string[] args)
        {
            // lambda 表达式
            //Func<double, double, double> func1 = (double a , double b) => { return a + b; };
            Func<double, double, double> func1 = (a, b) => { return a + b; };
            var result = func1(100.1, 200.2);
            Console.WriteLine(result);
        }
    }
}
           
c#