首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 2.0 泛型類別建立與使用 (2) - 從強型別集合轉換為泛型類別

     編輯:關於C語言

三、實作泛型類別

先定義強型別集合 CustomerList,再轉換為泛型 List<T>。

// 強型別版本
public class CustomerList : CollectionBase
{
    public Customer this[int index]
    {
        get { return (Customer)List[index]; }
        set { List[index] = value; }
    }
    public int Add(Customer value) { return List.Add(value); }
}

四、定義類別標頭

宣告型別參數 T:

public class List<T> : CollectionBase { }

五、實作泛型欄位

若有私有欄位 private int foo;,轉為 private T foo;,實例化時隨 T 具體化。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved