private void Reset(int capacity) { entries = new ArrayList(capacity); entriesTable = new Hashtable(capacity, hashCodeProvider, comparer); nullKeyEntry = null; version++; }
/// <summary> /// <para>Removes the entries with the specified key from the <see cref="DataCollection"/> instance.</para> /// </summary> /// <param name="name"> /// <para>The string key of the entry to add. The key can be a <see langword="null"/> reference (<see langword="Nothing"/> in Visual Basic)</para>. /// </param> /// <exception cref="NotSupportedException"> /// <para>The collection is read-only.</para> /// <para>- or -</para> /// <para>The collection has a fixed size.</para> /// </exception> protected void BaseRemove(String name) { CheckIfReadOnly(); if (name != null) { // remove from hashtable entriesTable.Remove(name); // remove from array for (int i = entries.Count - 1; i >= 0; i--) { if (comparer.Compare(name, BaseGetKey(i)) == 0) { entries.RemoveAt(i); } } } else { // null key -- special case // null out special 'null key' entry nullKeyEntry = null; // remove from array for (int i = entries.Count - 1; i >= 0; i--) { if (BaseGetKey(i) == null) { entries.RemoveAt(i); } } } version++; }
/// <summary> /// <para>Adds an entry with the specified key and value into the <see cref="DataCollection"/> instance.</para> /// </summary> /// <param name="name"> /// <para>The string key of the entry to add. The key can be a <see langword="null"/> reference (<see langword="Nothing"/> in Visual Basic)</para>. /// </param> /// <param name="value"> /// <para>The object value of the entry to add. The value can be a <see langword="null"/> reference (<see langword="Nothing"/> in Visual Basic).</para> /// </param> /// <exception cref="InvalidOperationException"> /// <para>The collection already contains an entry for <paramref name="name"/>.</para> /// </exception> /// <exception cref="NotSupportedException"> /// <para>The collection is read-only.</para> /// <para>- or -</para> /// <para>The collection has a fixed size.</para> /// </exception> protected void BaseAdd(String name, Object value) { if (readOnly) { throw new NotSupportedException(SR.ExceptionCollectionReadOnly); } DataObjectEntry entry = new DataObjectEntry(name, value); // insert entry into hashtable if (name != null) { if (entriesTable[name] == null) { entriesTable.Add(name, entry); } else { throw new InvalidOperationException(SR.ExceptionDuplicateEntry(name)); } } else { // null key -- special case -- hashtable doesn't like null keys if (nullKeyEntry == null) { nullKeyEntry = entry; } } // add entry to the list entries.Add(entry); version++; }
public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { ArgumentValidation.CheckForNullReference(info, "info"); info.AddValue("ReadOnly", readOnly); info.AddValue("HashProvider", hashCodeProvider, typeof(IHashCodeProvider)); info.AddValue("Comparer", comparer, typeof(IComparer)); int count = entries.Count; info.AddValue("Count", count); String[] keys = new String[count]; Object[] values = new Object[count]; for (int i = 0; i < count; i++) { DataObjectEntry entry = (DataObjectEntry)entries[i]; keys[i] = entry.Key; values[i] = entry.Value; } info.AddValue("Keys", keys, typeof(String[])); info.AddValue("Values", values, typeof(Object[])); }
/// <summary> /// <para>Sets the value of the entry at the specified index of the <see cref="DataCollection"/> instance.</para> /// </summary> /// <param name="index"> /// <para>The zero-based index of the entry to set.</para> /// </param> /// <param name="value"> /// <para>The object that represents the new value of the entry to set. The value can be a <see langword="null"/> reference (<see langword="Nothing"/> in Visual Basic).</para> /// </param> /// <exception cref="NotSupportedException"> /// <para>The collection is read-only.</para> /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <para><paramref name="index"/> is outside the valid range of indexes for the collection.</para> /// </exception> protected void BaseSet(int index, Object value) { CheckIfReadOnly(); DataObjectEntry entry = (DataObjectEntry)entries[index]; entry.Value = value; version++; }
/// <summary> /// <para>Sets the value of the first entry with the specified key in the <see cref="DataCollection"/> instance, if found; otherwise, adds an entry with the specified key and value into the <see cref="DataCollection"/> instance.</para> /// </summary> /// <param name="name"> /// <para>The string key of the entry to set. The key can be a <see langword="null"/> reference (<see langword="Nothing"/> in Visual Basic)</para>. /// </param> /// <param name="value"> /// <para>The object that represents the new value of the entry to set. The value can be a <see langword="null"/> reference (<see langword="Nothing"/> in Visual Basic).</para> /// </param> /// <exception cref="InvalidOperationException"> /// <para>The collection already contains an entry for <paramref name="name"/>.</para> /// </exception> /// <exception cref="NotSupportedException"> /// <para>The collection is read-only.</para> /// </exception> protected void BaseSet(String name, Object value) { CheckIfReadOnly(); DataObjectEntry entry = FindEntry(name); if (entry != null) { entry.Value = value; version++; } else { BaseAdd(name, value); } }
/// <summary> /// <para>Removes the entry at the specified index of the <see cref="DataCollection"/> instance.</para> /// </summary> /// <param name="index"> /// <para>The zero-based index of the entry to remove.</para> /// </param> /// <exception cref="ArgumentOutOfRangeException"> /// <para><paramref name="index"/> is outside the valid range of indexes for the collection.</para> /// </exception> /// <exception cref="NotSupportedException"> /// <para>The collection is read-only.</para> /// <para>- or -</para> /// <para>The collection has a fixed size.</para> /// </exception> protected void BaseRemoveAt(int index) { CheckIfReadOnly(); String key = BaseGetKey(index); if (key != null) { // remove from hashtable entriesTable.Remove(key); } else { // null key -- special case // null out special 'null key' entry nullKeyEntry = null; } // remove from array entries.RemoveAt(index); version++; }
/// <summary> /// <para>Gets the key of the entry at the specified index of the <see cref="DataCollection"/> instance.</para> /// </summary> /// <param name="index"> /// <para>The zero-based index of the key to get.</para> /// </param> /// <returns> /// <para>A string that represents the key of the entry at the specified index.</para> /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// <para><paramref name="index"/> is outside the valid range of indexes for the collection.</para> /// </exception> protected String BaseGetKey(int index) { DataObjectEntry entry = (DataObjectEntry)entries[index]; return(entry.Key); }
/// <summary> /// <para>Gets the value of the first entry with the specified key from the <see cref="DataCollection"/> instance.</para> /// </summary> /// <param name="name"> /// <para>The string key of the entry to add. The key can be a <see langword="null"/> reference (<see langword="Nothing"/> in Visual Basic)</para>. /// </param> /// <returns> /// <para>An object that represents the value of the first entry with the specified key, if found; otherwise, a <see langword="null"/> reference (<see langword="Nothing"/> in Visual Basic).</para> /// </returns> protected Object BaseGet(String name) { DataObjectEntry e = FindEntry(name); return((e != null) ? e.Value : null); }
/// <summary> /// <para>Gets the value of the entry at the specified index of the <see cref="DataCollection"/> instance.</para> /// </summary> /// <param name="index"> /// <para>The zero-based index of the value to get.</para> /// </param> /// <returns> /// <para>An object that represents the value of the entry at the specified index.</para> /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// <para><paramref name="index"/> is outside the valid range of indexes for the collection.</para> /// </exception> protected Object BaseGet(int index) { DataObjectEntry entry = (DataObjectEntry)entries[index]; return(entry.Value); }