程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#泛型秘訣(5.1)(3)

C#泛型秘訣(5.1)(3)

編輯:關於C語言

構造方法#region 構造方法

  //類型構造器
  static ReversibleSortedList()
  {
    ReversibleSortedList<TKey, TValue>.emptyKeys = new TKey[0];
    ReversibleSortedList<TKey, TValue>.emptyValues = new TValue[0];
  }
  //無參構造方法
  public ReversibleSortedList()
  {
    this.keys = ReversibleSortedList<TKey, TValue>.emptyKeys;
    this.values = ReversibleSortedList<TKey, TValue>.emptyValues;
    this._size = 0;
    this._sortDirectionComparer = new SortDirectionComparer<TKey>();
    this._currentSortDirection = this._sortDirectionComparer.SortDirection;
  }
  //用於指定排序方向的構造方法
  public ReversibleSortedList(SortDirectionComparer<TKey> comparer)
    : this()
  {
    if (comparer != null)
    {
      this._sortDirectionComparer = comparer;
      this._currentSortDirection = _sortDirectionComparer.SortDirection;
    }
  }
  //用於指定字典的構造方法
  public ReversibleSortedList(IDictionary<TKey, TValue> dictionary)
    : this(dictionary, (SortDirectionComparer<TKey>)null)
  {
  }
  //用於指定列表容量的構造方法
  public ReversibleSortedList(int capacity)
  {
    if (capacity < 0)
    {
      throw new ArgumentOutOfRangeException(
        "capacity", "Non-negative number required");
    }
    this.keys = new TKey[capacity];
    this.values = new TValue[capacity];
    this._sortDirectionComparer = new SortDirectionComparer<TKey>();
    this._currentSortDirection = _sortDirectionComparer.SortDirection;
  }
  //用於指定字典和排序方向的構造方法
  public ReversibleSortedList(IDictionary<TKey, TValue> dictionary,
                SortDirectionComparer<TKey> comparer)
    : this((dictionary != null) ? dictionary.Count : 0, comparer)
  {
    if (dictionary == null)
    {
      throw new ArgumentNullException("dictionary");
    }
    dictionary.Keys.CopyTo(this.keys, 0);
    dictionary.Values.CopyTo(this.values, 0);
    Array.Sort<TKey, TValue>(this.keys, this.values,
                    this._sortDirectionComparer);
    this._size = dictionary.Count;
  }
  //用於指定容量和排序方向的構造方法
  public ReversibleSortedList(int capacity, SortDirectionComparer<TKey> comparer)
    : this(comparer)
  {
    this.Capacity = capacity;
  }
  #endregion //CTORS

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved