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

C#泛型秘訣(5.2)

編輯:關於C#

私有方法#region 私有方法

private void EnsureCapacity(int min)
  {
  int num1 = (this.keys.Length == 0) ? 4 : (this.keys.Length * 2);
  if (num1 < min)
  {
  num1 = min;
  }
  this.InternalSetCapacity(num1, false);
  }
  //返回指定索引的值
  private TValue GetByIndex(int index)
  {
  if ((index < 0) || (index >= this._size))
  {
  throw new ArgumentOutOfRangeException("index", "Index out of range");
  }
  return this.values[index];
  }
  //返回指定索引的鍵
  private TKey GetKey(int index)
  {
  if ((index < 0) || (index >= this._size))
  {
  throw new ArgumentOutOfRangeException("index", "Index out of range");
  }
  return this.keys[index];
  }
  private KeyList<TKey, TValue> GetKeyListHelper()
  {
    if (this.keyList == null)
    {
      this.keyList = new KeyList<TKey, TValue>(this);
    }
    return this.keyList;
  }
  private ValueList<TKey, TValue> GetValueListHelper()
  {
    if (this.valueList == null)
    {
      this.valueList = new ValueList<TKey, TValue>(this);
    }
    return this.valueList;
  }
  //在指定位置插入元素
  private void Insert(int index, TKey key, TValue value)
  {
    if (this._size == this.keys.Length)
    {
      this.EnsureCapacity(this._size + 1);
    }
    if (index < this._size)
    {
      Array.Copy(this.keys, index, this.keys, (int)(index + 1),
             (int)(this._size - index));
      Array.Copy(this.values, index, this.values, (int)(index + 1),
             (int)(this._size - index));
    }
    this.keys[index] = key;
    this.values[index] = value;
    this._size++;
    this.version++;
  }
  private void InternalSetCapacity(int value, bool updateVersion)
  {
    if (value != this.keys.Length)
    {
      if (value < this._size)
      {
        throw new ArgumentOutOfRangeException(
          "value", "Too small capacity");
      }
      if (value > 0)
      {
        TKey[] localArray1 = new TKey[value];
        TValue[] localArray2 = new TValue[value];
        if (this._size > 0)
        {
          Array.Copy(this.keys, 0, localArray1, 0, this._size);
          Array.Copy(this.values, 0, localArray2, 0, this._size);
        }
        this.keys = localArray1;
        this.values = localArray2;
      }
      else
      {
        this.keys = ReversibleSortedList<TKey, TValue>.emptyKeys;
        this.values = ReversibleSortedList<TKey, TValue>.emptyValues;
      }
      if (updateVersion)
      {
        this.version++;
      }
    }
  }
  private static bool IsCompatibleKey(object key)
  {
    if (key.Equals(null))
    {
      throw new ArgumentNullException("key");
    }
    return (key is TKey);
  }
  //顯式接口成員實現
  void ICollection<KeyValuePair<TKey, TValue>>.Add(
                      KeyValuePair<TKey, TValue> keyValuePair)
  {
    this.Add(keyValuePair.Key, keyValuePair.Value);
  }
  //顯式接口成員實現
  bool ICollection<KeyValuePair<TKey, TValue>>.Contains(
                         KeyValuePair<TKey, TValue> keyValuePair)
  {
    int num1 = this.IndexOfKey(keyValuePair.Key);
    if ((num1 >= 0) && EqualityComparer<TValue>.Default.Equals(this.values[num1],
                                  keyValuePair.Value))
    {
      return true;
    }
    return false;
  }
  //顯式接口成員實現
  void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(
    KeyValuePair<TKey,TValue>[] array, int arrayIndex)
  {
    if (array == null)
    {
      throw new ArgumentNullException("array");
    }
    if ((arrayIndex < 0) || (arrayIndex > array.Length))
    {
      throw new ArgumentOutOfRangeException(
         "arrayIndex", "Need a non-negative number");
    }
    if ((array.Length - arrayIndex) < this.Count)
    {
      throw new ArgumentException("ArrayPlusOffTooSmall");
    }
    for (int num1 = 0; num1 < this.Count; num1++)
    {
      KeyValuePair<TKey, TValue> pair1;
      pair1 = new KeyValuePair<TKey, TValue>(
            this.keys[num1], this.values[num1]);
      array[arrayIndex + num1] = pair1;
    }
  }
  //顯式接口成員實現
  bool ICollection<KeyValuePair<TKey, TValue>>.Remove(
    KeyValuePair<TKey, TValue> keyValuePair)
  {
    int num1 = this.IndexOfKey(keyValuePair.Key);
    if ((num1 >= 0) && EqualityComparer<TValue>.Default.Equals(
      this.values[num1], keyValuePair.Value))
    {
      this.RemoveAt(num1);
      return true;
    }
    return false;
  }
  IEnumerator<KeyValuePair<TKey, TValue>>
     IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
  {
    return new ReversibleSortedList<TKey, TValue>.Enumerator<TKey, TValue>(
          this);
  }
  //顯式接口成員實現
  void ICollection.CopyTo(Array array, int arrayIndex)
  {
    if (array == null)
    {
      throw new ArgumentNullException("array");
    }
    if (array.Rank != 1)
    {
      throw new ArgumentException(
        "MultiDimensional array copies are not supported");
    }
    if (array.GetLowerBound(0) != 0)
    {
      throw new ArgumentException("A non-zero lower bound was provided");
    }
    if ((arrayIndex < 0) || (arrayIndex > array.Length))
    {
      throw new ArgumentOutOfRangeException(
        "arrayIndex", "Need non negative number");
    }
    if ((array.Length - arrayIndex) < this.Count)
    {
      throw new ArgumentException("Array plus the offset is too small");
    }
    KeyValuePair<TKey, TValue>[] pairArray1 =
       array as KeyValuePair<TKey, TValue>[];
    if (pairArray1 != null)
    {
      for (int num1 = 0; num1 < this.Count; num1++)
      {
        pairArray1[num1 + arrayIndex] =
          new KeyValuePair<TKey, TValue>(this.keys[num1],
          this.values[num1]);
      }
    }
    else
    {
      object[] objArray1 = array as object[];
      if (objArray1 == null)
      {
        throw new ArgumentException("Invalid array type");
      }
      try
      {
        for (int num2 = 0; num2 < this.Count; num2++)
        {
          objArray1[num2 + arrayIndex] =
              new KeyValuePair<TKey, TValue>(this.keys[num2],
                                this.values[num2]);
        }
      }
      catch (ArrayTypeMismatchException)
      {
        throw new ArgumentException("Invalid array type");
      }
    }
  }
  //顯式接口成員實現
  void IDictionary.Add(object key, object value)
  {
    ReversibleSortedList<TKey, TValue>.VerifyKey(key);
    ReversibleSortedList<TKey, TValue>.VerifyValueType(value);
    this.Add((TKey)key, (TValue)value);
  }
  //顯式接口成員實現
  bool IDictionary.Contains(object key)
  {
    if (ReversibleSortedList<TKey, TValue>.IsCompatibleKey(key))
    {
      return this.ContainsKey((TKey)key);
    }
    return false;
  }
  //顯式接口成員實現
  IDictionaryEnumerator IDictionary.GetEnumerator()
  {
    return new ReversibleSortedList<TKey, TValue>.Enumerator<TKey, TValue>(
        this);
  }
  //顯式接口成員實現
  void IDictionary.Remove(object key)
  {
    if (ReversibleSortedList<TKey, TValue>.IsCompatibleKey(key))
    {
      this.Remove((TKey)key);
    }
  }
  //顯式接口成員實現
  IEnumerator IEnumerable.GetEnumerator()
  {
    return new ReversibleSortedList<TKey, TValue>.Enumerator<TKey, TValue>(
        this);
  }
  private static void VerifyKey(object key)
  {
    if (key.Equals(null))
    {
      throw new ArgumentNullException("key");
    }
    if (!(key is TKey))
    {
      throw new ArgumentException(
        "Argument passed is of wrong type", "key");
    }
  }
  private static void VerifyValueType(object value)
  {
    if (!(value is TValue) && ((value != null) || typeof(TValue).IsValueType))
    {
      throw new ArgumentException(
        "Argument passed is of wrong type", "value");
    }
  }
  #endregion // Private methods
  Public Properties#region Public Properties
  public int Capacity
  {
    get
    {
      return this.keys.Length;
    }
    set
    {
      this.InternalSetCapacity(value, true);
    }
  }
  public SortDirectionComparer<TKey> Comparer
  {
    get
    {
      return this._sortDirectionComparer;
    }
  }
  public int Count
  {
    get
    {
      return this._size;
    }
  }
  public TValue this[TKey key]
  {
    get
    {
      TValue local1;
      int num1 = this.IndexOfKey(key);
      if (num1 >= 0)
      {
        return this.values[num1];
      }
      else
      {
        //throw new KeyNotFoundException();
        local1 = default(TValue);
        return local1;
      }
    }
    set
    {
      if (key == null)
      {
        throw new ArgumentNullException("key");
      }
      int num1 = Array.BinarySearch<TKey>(this.keys, 0, this._size, key,
                            this._sortDirectionComparer);
      if (num1 >= 0)
      {
        this.values[num1] = value;
        this.version++;
      }
      else
      {
        this.Insert(~num1, key, value);
      }
    }
  }
  public IList<TKey> Keys
  {
    get
    {
      return this.GetKeyListHelper();
    }
  }
  public IList<TValue> Values
  {
    get
    {
      return this.GetValueListHelper();
    }
  }
  #endregion // Public Properties
  Private Properties#region Private Properties
  bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
  {
    get
    {
      return false;
    }
  }
  ICollection<TKey> IDictionary<TKey, TValue>.Keys
  {
    get
    {
      return this.GetKeyListHelper();
    }
  }
  ICollection<TValue> IDictionary<TKey, TValue>.Values
  {
    get
    {
      return this.GetValueListHelper();
    }
  }
  bool ICollection.IsSynchronized
  {
    get
    {
      return false;
    }
  }
  object ICollection.SyncRoot
  {
    get
    {
      return this;
    }
  }
  bool IDictionary.IsFixedSize
  {
    get
    {
      return false;
    }
  }
  bool IDictionary.IsReadOnly
  {
    get
    {
      return false;
    }
  }
  object IDictionary.this[object key]
  {
    get
    {
      if (ReversibleSortedList<TKey, TValue>.IsCompatibleKey(key))
      {
        int num1 = this.IndexOfKey((TKey)key);
        if (num1 >= 0)
        {
          return this.values[num1];
        }
      }
      return null;
    }
    set
    {
      ReversibleSortedList<TKey, TValue>.VerifyKey(key);
      ReversibleSortedList<TKey, TValue>.VerifyValueType(value);
      this[(TKey)key] = (TValue)value;
    }
  }
  ICollection IDictionary.Keys
  {
    get
    {
      return this.GetKeyListHelper();
    }
  }
  ICollection IDictionary.Values
  {
    get
    {
      return this.GetValueListHelper();
    }
  }
  #endregion // Private properties
  Fields#region Fields
  private const int _defaultCapacity = 4;
  private int _size;
  //private IComparer<TKey> comparer;
  private static TKey[] emptyKeys;
  private static TValue[] emptyValues;
  private KeyList<TKey, TValue> keyList;
  private TKey[] keys;
  private ValueList<TKey, TValue> valueList;
  private TValue[] values;
  private int version;
  // Declare comparison object.
  private SortDirectionComparer<TKey> _sortDirectionComparer = null;
  // Default to ascending.
  private ListSortDirection _currentSortDirection = ListSortDirection.Descending;
  #endregion

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