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

C# 泛型基礎:StandardClass 非泛型類別架構分析

     編輯:關於C語言

前言

在深入泛型世界前,先理解非泛型集合類別的設計缺陷,有助於體會泛型解決的核心問題。本文剖析 StandardClass 這個典型的「以 object 為基礎」集合類別,展示其結構與運作方式,為後續泛型改良鋪路。

StandardClass 完整實作

public class StandardClass
{
    static int _count = 0;           // 類別層級實例計數器
    int _maxItemCount;               // 最大容量
    object[] _items;                 // 核心:object 陣列
    int _currentItem = 0;            // 目前元素數量

    public StandardClass(int items)
    {
        _count++;
        _maxItemCount = items;
        _items = new object[_maxItemCount];  // 建立 object 陣列
    }

    // 加入元素:接受 object,任何型別皆可
    public int AddItem(object item)
    {
        if (_currentItem < _maxItemCount)
        {
            _items[_currentItem] = item;
            return _currentItem++;
        }
        throw new Exception("Item queue is full");
    }

    // 取得元素:回傳 object,呼叫端需自行轉型
    public object GetItem(int index)
    {
        if (index >= _maxItemCount)
            throw new ArgumentOutOfRangeException("index");
        return _items[index];
    }

    public int ItemCount => _currentItem;

    public override string ToString()
    {
        return $"There are {_count} instances of {this.GetType()} " +
               $"which contains {_currentItem} items of type {_items.GetType()}";
    }
}

架構特點分析

1. 靜態計數器 _count

屬於類別層級,記錄所有 StandardClass 實例總數。每次建構子呼叫遞增,展示類別生命週期追蹤機制。

2. object 陣列 _items

核心設計決策:使用 object[] 陣列儲存元素。因為所有型別(參考型別與值型別)皆隱含繼承自 object,單一陣列即可容納任意型別混合。

  • 優點:極大彈性,單一類別支援所有型別
  • 缺點:值型別存入需裝箱、取出需拆箱;失去編譯期型別檢查;陣列元素實際型別資訊在執行期才可知

3. AddItem / GetItem 方法

// 參數與回傳皆為 object
public int AddItem(object item) { ... }
public object GetItem(int index) { ... }

這迫使呼叫端在取得元素時進行強制型別轉換:

string s = (string)stdClass.GetItem(0);  // 執行期可能拋出例外

4. ToString() 顯示資訊


// 輸出範例
There are 2 instances of StandardClass which contains 3 items of type System.Object[]

注意:_items.GetType() 永遠回傳 System.Object[],無法得知實際儲存的元素型別。

存在的三大問題

問題說明後果
型別不安全 編譯器允許 AddItem(123) 加入整數到字串集合 執行期 InvalidCastException
效能損耗 值型別每次存取皆產生裝箱/拆箱 記憶體壓力增加、CPU 週期浪費
程式碼雜亂 到處充滿 (Type)obj 強制轉型 可讀性差、維護困難、易出錯

為什麼需要泛型?

StandardClass 展示了「以 object 為基礎」集合的極限:用極大的彈性換取了型別安全與效能。泛型的誕生,正是要在「保持彈性」與「獲得型別安全/效能」之間找到平衡點——透過參數化型別,讓編譯器與執行期都知道集合裝的是什麼型別。

總結

StandardClass 是理解泛型動機的完美反面教材。它的每個設計選擇(object[]、object 參數/回傳),在泛型版本 GenericClass<T> 中都被對應的 T[]、T 參數、T 回傳所取代。這三處替換,構成了泛型類別設計的核心模式。

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