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

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

編輯:關於C語言

公有方法#region 公有方法

//添加元素
  public void Add(TKey key, TValue value)
  {
    if (key.Equals(null))
    {
      throw new ArgumentNullException("key");
    }
    int num1 = Array.BinarySearch<TKey>(this.keys, 0, this._size, key,
                        this._sortDirectionComparer);
    if (num1 >= 0)
    {
      throw new ArgumentException("Attempting to add duplicate");
    }
    this.Insert(~num1, key, value);
  }
  //ICollection<KeyValuePair<TKey, TValue>>接口方法實現
  public void Clear()
  {
    this.version++;
    Array.Clear(this.keys, 0, this._size);
    Array.Clear(this.values, 0, this._size);
    this._size = 0;
  }
  //判斷是否包含指定鍵
  public bool ContainsKey(TKey key)
  {
    return (this.IndexOfKey(key) >= 0);
  }
  //判斷是否包含指定值
  public bool ContainsValue(TValue value)
  {
    return (this.IndexOfValue(value) >= 0);
  }

  public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  {
    return new ReversibleSortedList<TKey, TValue>.Enumerator<TKey, TValue>(
          this);
  }
  //查找指定鍵
  public int IndexOfKey(TKey key)
  {

    if (key.Equals(null))
    {
      throw new ArgumentNullException("key");
    }
    int num1 = Array.BinarySearch<TKey>(this.keys, 0, this._size, key,
                        this._sortDirectionComparer);
    if (num1 < 0)
    {
      return -1;
    }
    return num1;
  }
  //查找指定值
  public int IndexOfValue(TValue value)
  {
    return Array.IndexOf<TValue>(this.values, value, 0, this._size);
  }
  //IDictionary<TKey, TValue>接口方法實現
  public bool Remove(TKey key)
  {
    int num1 = this.IndexOfKey(key);
    if (num1 >= 0)
    {
      this.RemoveAt(num1);
    }
    return (num1 >= 0);
  }
  //移除指定索引元素
  public void RemoveAt(int index)
  {
    if ((index < 0) || (index >= this._size))
    {
      throw new ArgumentOutOfRangeException("index", "Index out of range");
    }
    this._size--;
    if (index < this._size)
    {
      Array.Copy(this.keys, (int)(index + 1), this.keys, index,
            (int)(this._size - index));
      Array.Copy(this.values, (int)(index + 1), this.values, index,
            (int)(this._size - index));
    }
    this.keys[this._size] = default(TKey);
    this.values[this._size] = default(TValue);
    this.version++;
  }
  //排序
  public void Sort()
  {
    // 檢查是否跟現有排序方向相同.
    if (this._currentSortDirection !=
      this._sortDirectionComparer.SortDirection)
    {
      // 如果不同,則進行反轉.
      Array.Reverse(this.keys, 0, this._size);
      Array.Reverse(this.values, 0, this._size);
      // 設置當前排序.
      this._currentSortDirection = this._sortDirectionComparer.SortDirection;
    }
  }
  //剪除多余空間
  public void TrimExcess()
  {
    int num1 = (int)(this.keys.Length * 0.9);
    if (this._size < num1)
    {
      this.Capacity = this._size;
    }
  }
  //獲取指定鍵的值
  public bool TryGetValue(TKey key, out TValue value)
  {
    int num1 = this.IndexOfKey(key);
    if (num1 >= 0)
    {
      value = this.values[num1];
      return true;
    }
    value = default(TValue);
    return false;
  }

  #endregion // Public Methods

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