NameObjectCollectionBase Class

 

Provides the abstract base class for a collection of associated String keys and Object values that can be accessed either with the key or with the index.

Namespace:   System.Collections.Specialized
Assembly:  System (in System.dll)


[SerializableAttribute]
public abstract class NameObjectCollectionBase : ICollection, 
	IEnumerable, ISerializable, IDeserializationCallback

NameDescription
System_CAPS_protmethodNameObjectCollectionBase()

Initializes a new instance of the NameObjectCollectionBase class that is empty.

System_CAPS_protmethodNameObjectCollectionBase(IEqualityComparer)

Initializes a new instance of the NameObjectCollectionBase class that is empty, has the default initial capacity, and uses the specified IEqualityComparer object.

System_CAPS_protmethodNameObjectCollectionBase(IHashCodeProvider, IComparer)

Obsolete.Initializes a new instance of the NameObjectCollectionBase class that is empty, has the default initial capacity, and uses the specified hash code provider and the specified comparer.

System_CAPS_protmethodNameObjectCollectionBase(Int32)

Initializes a new instance of the NameObjectCollectionBase class that is empty, has the specified initial capacity, and uses the default hash code provider and the default comparer.

System_CAPS_protmethodNameObjectCollectionBase(Int32, IEqualityComparer)

Initializes a new instance of the NameObjectCollectionBase class that is empty, has the specified initial capacity, and uses the specified IEqualityComparer object.

System_CAPS_protmethodNameObjectCollectionBase(Int32, IHashCodeProvider, IComparer)

Obsolete.Initializes a new instance of the NameObjectCollectionBase class that is empty, has the specified initial capacity and uses the specified hash code provider and the specified comparer.

System_CAPS_protmethodNameObjectCollectionBase(SerializationInfo, StreamingContext)

Initializes a new instance of the NameObjectCollectionBase class that is serializable and uses the specified System.Runtime.Serialization.SerializationInfo and System.Runtime.Serialization.StreamingContext.

NameDescription
System_CAPS_pubpropertyCount

Gets the number of key/value pairs contained in the NameObjectCollectionBase instance.

System_CAPS_protpropertyIsReadOnly

Gets or sets a value indicating whether the NameObjectCollectionBase instance is read-only.

System_CAPS_pubpropertyKeys

Gets a NameObjectCollectionBase.KeysCollection instance that contains all the keys in the NameObjectCollectionBase instance.

NameDescription
System_CAPS_protmethodBaseAdd(String, Object)

Adds an entry with the specified key and value into the NameObjectCollectionBase instance.

System_CAPS_protmethodBaseClear()

Removes all entries from the NameObjectCollectionBase instance.

System_CAPS_protmethodBaseGet(Int32)

Gets the value of the entry at the specified index of the NameObjectCollectionBase instance.

System_CAPS_protmethodBaseGet(String)

Gets the value of the first entry with the specified key from the NameObjectCollectionBase instance.

System_CAPS_protmethodBaseGetAllKeys()

Returns a String array that contains all the keys in the NameObjectCollectionBase instance.

System_CAPS_protmethodBaseGetAllValues()

Returns an Object array that contains all the values in the NameObjectCollectionBase instance.

System_CAPS_protmethodBaseGetAllValues(Type)

Returns an array of the specified type that contains all the values in the NameObjectCollectionBase instance.

System_CAPS_protmethodBaseGetKey(Int32)

Gets the key of the entry at the specified index of the NameObjectCollectionBase instance.

System_CAPS_protmethodBaseHasKeys()

Gets a value indicating whether the NameObjectCollectionBase instance contains entries whose keys are not null.

System_CAPS_protmethodBaseRemove(String)

Removes the entries with the specified key from the NameObjectCollectionBase instance.

System_CAPS_protmethodBaseRemoveAt(Int32)

Removes the entry at the specified index of the NameObjectCollectionBase instance.

System_CAPS_protmethodBaseSet(Int32, Object)

Sets the value of the entry at the specified index of the NameObjectCollectionBase instance.

System_CAPS_protmethodBaseSet(String, Object)

Sets the value of the first entry with the specified key in the NameObjectCollectionBase instance, if found; otherwise, adds an entry with the specified key and value into the NameObjectCollectionBase instance.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetEnumerator()

Returns an enumerator that iterates through the NameObjectCollectionBase.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetObjectData(SerializationInfo, StreamingContext)

Implements the ISerializable interface and returns the data needed to serialize the NameObjectCollectionBase instance.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodOnDeserialization(Object)

Implements the ISerializable interface and raises the deserialization event when the deserialization is complete.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodICollection.CopyTo(Array, Int32)

Copies the entire NameObjectCollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.IsSynchronized

Gets a value indicating whether access to the NameObjectCollectionBase object is synchronized (thread safe).

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.SyncRoot

Gets an object that can be used to synchronize access to the NameObjectCollectionBase object.

NameDescription
System_CAPS_pubmethodAsParallel()

Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)

System_CAPS_pubmethodAsQueryable()

Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)

System_CAPS_pubmethodCast<TResult>()

Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)

System_CAPS_pubmethodOfType<TResult>()

Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

The underlying structure for this class is a hash table.

Each element is a key/value pair.

The capacity of a NameObjectCollectionBase is the number of elements the NameObjectCollectionBase can hold. As elements are added to a NameObjectCollectionBase, the capacity is automatically increased as required through reallocation.

The hash code provider dispenses hash codes for keys in the NameObjectCollectionBase instance. The default hash code provider is the CaseInsensitiveHashCodeProvider.

The comparer determines whether two keys are equal. The default comparer is the CaseInsensitiveComparer.

In .NET Framework version 1.0, this class uses culture-sensitive string comparisons. However, in .NET Framework version 1.1 and later, this class uses CultureInfo.InvariantCulture when comparing strings. For more information about how culture affects comparisons and sorting, see Performing Culture-Insensitive String Operations.

null is allowed as a key or as a value.

System_CAPS_cautionCaution

The BaseGet method does not distinguish between null which is returned because the specified key is not found and null which is returned because the value associated with the key is null.

The following code example shows how to implement and use the NameObjectCollectionBase class.

using System;
using System.Collections;
using System.Collections.Specialized;

public class MyCollection : NameObjectCollectionBase
{
   // Creates an empty collection.
   public MyCollection()  {
   }

   // Adds elements from an IDictionary into the new collection.
   public MyCollection( IDictionary d, Boolean bReadOnly )  {
      foreach ( DictionaryEntry de in d )  {
         this.BaseAdd( (String) de.Key, de.Value );
      }
      this.IsReadOnly = bReadOnly;
   }

   // Gets a key-and-value pair (DictionaryEntry) using an index.
   public DictionaryEntry this[ int index ]  {
      get  {
          return ( new DictionaryEntry( 
              this.BaseGetKey(index), this.BaseGet(index) ) );
      }
   }

   // Gets or sets the value associated with the specified key.
   public Object this[ String key ]  {
      get  {
         return( this.BaseGet( key ) );
      }
      set  {
         this.BaseSet( key, value );
      }
   }

   // Gets a String array that contains all the keys in the collection.
   public String[] AllKeys  {
      get  {
         return( this.BaseGetAllKeys() );
      }
   }

   // Gets an Object array that contains all the values in the collection.
   public Array AllValues  {
      get  {
         return( this.BaseGetAllValues() );
      }
   }

   // Gets a String array that contains all the values in the collection.
   public String[] AllStringValues  {
      get  {
         return( (String[]) this.BaseGetAllValues( typeof( string ) ));
      }
   }

   // Gets a value indicating if the collection contains keys that are not null.
   public Boolean HasKeys  {
      get  {
         return( this.BaseHasKeys() );
      }
   }

   // Adds an entry to the collection.
   public void Add( String key, Object value )  {
      this.BaseAdd( key, value );
   }

   // Removes an entry with the specified key from the collection.
   public void Remove( String key )  {
      this.BaseRemove( key );
   }

   // Removes an entry in the specified index from the collection.
   public void Remove( int index )  {
      this.BaseRemoveAt( index );
   }

   // Clears all the elements in the collection.
   public void Clear()  {
      this.BaseClear();
   }

}

public class SamplesNameObjectCollectionBase  {

   public static void Main()  {

      // Creates and initializes a new MyCollection that is read-only.
      IDictionary d = new ListDictionary();
      d.Add( "red", "apple" );
      d.Add( "yellow", "banana" );
      d.Add( "green", "pear" );
      MyCollection myROCol = new MyCollection( d, true );

      // Tries to add a new item.
      try  {
         myROCol.Add( "blue", "sky" );
      }
      catch ( NotSupportedException e )  {
         Console.WriteLine( e.ToString() );
      }

      // Displays the keys and values of the MyCollection.
      Console.WriteLine( "Read-Only Collection:" );
      PrintKeysAndValues( myROCol );


      // Creates and initializes an empty MyCollection that is writable.
      MyCollection myRWCol = new MyCollection();

      // Adds new items to the collection.
      myRWCol.Add( "purple", "grape" );
      myRWCol.Add( "orange", "tangerine" );
      myRWCol.Add( "black", "berries" );
      Console.WriteLine( "Writable Collection (after adding values):" );
      PrintKeysAndValues( myRWCol );

      // Changes the value of one element.
      myRWCol["orange"] = "grapefruit";
      Console.WriteLine( "Writable Collection (after changing one value):" );
      PrintKeysAndValues( myRWCol );

      // Removes one item from the collection.
      myRWCol.Remove( "black" );
      Console.WriteLine( "Writable Collection (after removing one value):" );
      PrintKeysAndValues( myRWCol );

      // Removes all elements from the collection.
      myRWCol.Clear();
      Console.WriteLine( "Writable Collection (after clearing the collection):" );
      PrintKeysAndValues( myRWCol );

   }

   // Prints the indexes, keys, and values.
   public static void PrintKeysAndValues( MyCollection myCol )  {
      for ( int i = 0; i < myCol.Count; i++ )  {
         Console.WriteLine( "[{0}] : {1}, {2}", i, myCol[i].Key, myCol[i].Value );
      }
   }

   // Prints the keys and values using AllKeys.
   public static void PrintKeysAndValues2( MyCollection myCol )  {
      foreach ( String s in myCol.AllKeys )  {
         Console.WriteLine( "{0}, {1}", s, myCol[s] );
      }
   }
}


/*
This code produces the following output.

System.NotSupportedException: Collection is read-only.
   at System.Collections.Specialized.NameObjectCollectionBase.BaseAdd(String name, Object value)
   at SamplesNameObjectCollectionBase.Main()
Read-Only Collection:
[0] : red, apple
[1] : yellow, banana
[2] : green, pear
Writable Collection (after adding values):
[0] : purple, grape
[1] : orange, tangerine
[2] : black, berries
Writable Collection (after changing one value):
[0] : purple, grape
[1] : orange, grapefruit
[2] : black, berries
Writable Collection (after removing one value):
[0] : purple, grape
[1] : orange, grapefruit
Writable Collection (after clearing the collection):

*/

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

This implementation does not provide a synchronized (thread safe) wrapper for a NameObjectCollectionBase, but derived classes can create their own synchronized versions of the NameObjectCollectionBase using the SyncRoot property.

Enumerating through a collection is intrinsically not a thread safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

Return to top
Show: