10泛型

□ 什么时候使用泛型

当针对不同的数据类型,采用相似的逻辑算法,为了避免重复,可以考虑使用泛型。

  针对类的泛型

针对不同类型的数组,写一个针对数组的"冒泡排序"。

□ 思路

● 针对类的泛型,泛型打在类旁。
● 由于在"冒泡排序"中需要对元素进行比较,所以泛型要约束成实现IComparable接口。

   class Program
    {
        static void Main(string[] args)
        {
            SortHelper<int> isorter = new SortHelper<int>();
            int[] iarray = {8, 7, 1, 2, 12};
            isorter.BubbleSort(iarray);
            foreach (int  item in iarray)
            {
                Console.Write(item+ ", ");
            }
            Console.ReadKey();
        }
    }
 
    public class SortHelper<T> where T : IComparable
    {
        public void BubbleSort(T[] array) 
        {
            int length = array.Length;
            for (int i = 0; i <= length -2; i++)
            {
                for (int j = length - 1; j >= 1; j--)
                {
                    if (array[j].CompareTo(array[j-1]) < 0)
                    {
                        T temp = array[j];
                        array[j] = array[j - 1];
                        array[j - 1] = temp;
                    }
                }
            }
        }
    }
 

结果:
01

□ 关于泛型约束

where T : IComparable 把T约束为实现IComparable接口
where T : class
where T : struct
where T : IComparable, new() 约束泛型必须有构造函数

□ 关于冒泡算法

● 之所以for (int i = 0; i <= length -2; i++),这是边界思维,比如有一个长度为5的数组,如果0号位元素最终调换到4号位,每次调一个位,需要经过4次才能到4号位,即for(int i = 0; i <= 5-2, i++),i依次为0, 1, 2, 4,期间经历了4次。

● 至于for (int j = length - 1; j >= 1; j--)循环,即遍历从最后一个元素开始到索引为1的元素,每次与前一个位置上的元素比较。


□ 关于比较

int类型之所以能比较,是因为int类型也实现了IComparable接口。
02

byte类型也一样实现了IComparable接口。
03

  自定义一个类,使之也能实现冒泡算法

冒泡算法涉及到元素比较,所以自定义类必须实现IComparable接口。

   class Program
    {
        static void Main(string[] args)
        {
            Book[] bookArray = new Book[2];
            Book book1 = new Book(100, "书一");
            Book book2 = new Book(80, "书二");
            bookArray[0] = book1;
            bookArray[1] = book2;
 
            Console.WriteLine("冒泡之前:");
            foreach (Book b in bookArray)
            {
                Console.WriteLine("书名:{0},价格:{1}", b.Title, b.Price);
            }
 
            SortHelper<Book> sorter = new SortHelper<Book>();
            sorter.BubbleSort(bookArray);
            Console.WriteLine("冒泡之后:");
            foreach (Book b in bookArray)
            {
                Console.WriteLine("书名:{0},价格:{1}", b.Title, b.Price);
            }
            Console.ReadKey();
        }
    }
 
    public class SortHelper<T> where T : IComparable
    {
        public void BubbleSort(T[] array) 
        {
            int length = array.Length;
            for (int i = 0; i <= length -2; i++)
            {
                for (int j = length - 1; j >= 1; j--)
                {
                    if (array[j].CompareTo(array[j-1]) < 0)
                    {
                        T temp = array[j];
                        array[j] = array[j - 1];
                        array[j - 1] = temp;
                    }
                }
            }
        }
    }
 
    //自定义类实现IComparable接口
    public class Book : IComparable
    {
        private int price;
        private string title;
 
        public Book(){}
 
        public Book(int price, string title)
        {
            this.price = price;
            this.title = title;
        }
 
        public int Price
        {
            get { return this.price; }
        }
 
        public string Title
        {
            get { return this.title; }
        }
 
        public int CompareTo(object obj)
        {
            Book book = (Book)obj;
            return this.Price.CompareTo(book.Price);
        }
    }
 

结果:
04

  针对方法的泛型

继续上面的例子,自定义一个类,并定义泛型方法。

   //方法泛型
    public class MethodSortHelper
    {
        public void BubbleSort<T>(T[] array) where T : IComparable
        {
            int length = array.Length;
            for (int i = 0; i <= length - 2; i++)
            {
                for (int j = length - 1; j >= 1; j--)
                {
                    if (array[j].CompareTo(array[j - 1]) < 0)
                    {
                        T temp = array[j];
                        array[j] = array[j - 1];
                        array[j - 1] = temp;
                    }
                }
            }
        }
    }
 
主程序。
 
    class Program
    {
        static void Main(string[] args)
        {
            Book[] bookArray = new Book[2];
            Book book1 = new Book(100, "书一");
            Book book2 = new Book(80, "书二");
            bookArray[0] = book1;
            bookArray[1] = book2;
 
            Console.WriteLine("冒泡之前:");
            foreach (Book b in bookArray)
            {
                Console.WriteLine("书名:{0},价格:{1}", b.Title, b.Price);
            }
 
            MethodSortHelper sorter = new MethodSortHelper();
            sorter.BubbleSort<Book>(bookArray);
            Console.WriteLine("冒泡之后:");
            foreach (Book b in bookArray)
            {
                Console.WriteLine("书名:{0},价格:{1}", b.Title, b.Price);
            }
            Console.ReadKey();
        }
    }    
 

结果:
04

另外,使用泛型方法的时候,除了按以下:

            MethodSortHelper sorter = new MethodSortHelper();
            sorter.BubbleSort<Book>(bookArray);

还可以这样写:   

            MethodSortHelper sorter = new MethodSortHelper();
            sorter.BubbleSort(bookArray);       

可见,泛型方法可以根据数组实例隐式推断泛型是否满足条件。

  泛型的其它优点

□ 避免隐式装箱和拆箱

以下包含隐式装箱和拆箱:

ArrayList list = new ArrayList();
for(int i = 0; i < 3; i++)
{
    list.Add(i); //Add接收的参数类型是引用类型object,这里包含了隐式装箱
}
for(int i = 0; i < 3; i++)
{
    int value = (int)list[i]; //引用类型强转成值类型,拆箱
    Console.WriteLine(value);
}

使用泛型避免隐式装箱和拆箱:

List<int> list = new List<int>();
for(int i = 0; i < 3; i++)
{
    list.Add(i);
}
for(int i = 0; i < 3; i++)
{
    int value = list[i];
    Console.WriteLine(value);
}

□ 能在编译期间及时发现错误

不使用泛型,在编译期不会报错的一个例子:

ArrayList list = new ArrayList();
int i = 100;
list.Add(i);
string value = (string)list[0];

使用泛型,在编译期及时发现错误:

List<int> list = new List<int>();
int i = 100;
list.Add(i);
string value = (string)list[0];

  使用泛型的技巧

□ 在当前文件中给泛型取别名

using IntList = List<int>;
IntList list = new IntList();
list.Add(1);

□ 在不同文件中使用泛型别名,定义一个类派生于泛型

public class IntList : List<int>{}

参考资料:
《.NET之美》--张子阳,感谢写了这么好的书!

原文地址:https://www.cnblogs.com/darrenji/p/3603829.html