源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#泛型实例详解

  • 时间:2021-06-13 09:59 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#泛型实例详解
本文以实例形式讲述了C#泛型的用法,有助于读者深入理解C#泛型的原理,具体分析如下: 首先需要明白什么时候使用泛型: 当针对不同的数据类型,采用相似的逻辑算法,为了避免重复,可以考虑使用泛型。 [b]一、针对类的泛型[/b] 针对不同类型的数组,写一个针对数组的"冒泡排序"。 1.思路 ● 针对类的泛型,泛型打在类旁。 ● 由于在"冒泡排序"中需要对元素进行比较,所以泛型要约束成实现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;
          }
        }
      }
    }
  }
 
运行结果如下图所示: [img]http://files.jb51.net/file_images/article/201408/2014813112235234.png?2014713112446[/img] [b]2.关于泛型约束[/b] where T : IComparable 把T约束为实现IComparable接口 where T : class where T : struct where T : IComparable, new() 约束泛型必须有构造函数 [b]3.关于冒泡算法[/b] ● 之所以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的元素,每次与前一个位置上的元素比较。 4.关于比较 int类型之所以能比较,是因为int类型也实现了IComparable接口。 [img]http://files.jb51.net/file_images/article/201408/2014813112245142.png?2014713112433[/img] byte类型也一样实现了IComparable接口。 [img]http://files.jb51.net/file_images/article/201408/2014813112254598.png?2014713112421[/img] [b]二、自定义一个类,使之也能实现冒泡算法[/b] 冒泡算法涉及到元素比较,所以自定义类必须实现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);
    }
  }

运行结果如下图所示: [img]http://files.jb51.net/file_images/article/201408/2014813112302780.png?201471311246[/img] [b]三、针对方法的泛型[/b] 继续上面的例子,自定义一个类,并定义泛型方法。
  //方法泛型
  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();
    }
  }  
 
运行结果如下图所示: [img]http://files.jb51.net/file_images/article/201408/2014813112312122.png?2014713112335[/img] 另外,使用泛型方法的时候,除了按以下:
MethodSortHelper sorter = new MethodSortHelper(); 
sorter.BubbleSort<Book>(bookArray);
还可以这样写:  
      MethodSortHelper sorter = new MethodSortHelper(); 
      sorter.BubbleSort(bookArray); 
     
可见,泛型方法可以根据数组实例隐式推断泛型是否满足条件。 [b]四、泛型的其它优点[/b] 1.避免隐式装箱和拆箱 以下包含隐式装箱和拆箱:
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);
}

[b]2.能在编译期间及时发现错误[/b] 不使用泛型,在编译期不会报错的一个例子:
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];

[b]五、使用泛型的技巧[/b] 1.在当前文件中给泛型取别名
using IntList = List<int>;
IntList list = new IntList();
list.Add(1);

2.在不同文件中使用泛型别名,定义一个类派生于泛型
public class IntList : List<int>{}
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部