This documentation is archived and is not being maintained.

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 MustInherit Class NameObjectCollectionBase _
	Implements ICollection, IEnumerable, ISerializable, IDeserializationCallback
Dim instance As NameObjectCollectionBase

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 Comparing and Sorting Data for a Specific Culture Comparing and Sorting Data for a Specific Cultureand Performing Culture-Insensitive String Operations.

Nothing is allowed as a key or as a value.

Caution noteCaution:

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

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

Imports System
Imports System.Collections
Imports System.Collections.Specialized

Public Class MyCollection
   Inherits NameObjectCollectionBase

   ' Creates an empty collection. 
   Public Sub New()
   End Sub 'New 

   ' Adds elements from an IDictionary into the new collection. 
   Public Sub New(d As IDictionary, bReadOnly As Boolean)
      Dim de As DictionaryEntry
      For Each de In  d
         Me.BaseAdd(CType(de.Key, String), de.Value)
      Next de
      Me.IsReadOnly = bReadOnly
   End Sub 'New 

   ' Gets a key-and-value pair (DictionaryEntry) using an index.
   Default Public ReadOnly Property Item(index As Integer) As DictionaryEntry
            return new DictionaryEntry( _
                me.BaseGetKey(index), me.BaseGet(index) )
      End Get 
   End Property 

   ' Gets or sets the value associated with the specified key.
   Default Public Property Item(key As String) As Object
         Return Me.BaseGet(key)
      End Get 
         Me.BaseSet(key, value)
      End Set 
   End Property 

   ' Gets a String array that contains all the keys in the collection. 
   Public ReadOnly Property AllKeys() As String()
         Return Me.BaseGetAllKeys()
      End Get 
   End Property 

   ' Gets an Object array that contains all the values in the collection. 
   Public ReadOnly Property AllValues() As Array
         Return Me.BaseGetAllValues()
      End Get 
   End Property 

   ' Gets a String array that contains all the values in the collection. 
   Public ReadOnly Property AllStringValues() As String()
         Return CType(Me.BaseGetAllValues(GetType(String)), String())
      End Get 
   End Property 

   ' Gets a value indicating if the collection contains keys that are not null. 
   Public ReadOnly Property HasKeys() As Boolean 
         Return Me.BaseHasKeys()
      End Get 
   End Property 

   ' Adds an entry to the collection. 
   Public Sub Add(key As String, value As Object)
      Me.BaseAdd(key, value)
   End Sub 'Add

   ' Removes an entry with the specified key from the collection. 
   Overloads Public Sub Remove(key As String)
   End Sub 'Remove

   ' Removes an entry in the specified index from the collection. 
   Overloads Public Sub Remove(index As Integer)
   End Sub 'Remove

   ' Clears all the elements in the collection. 
   Public Sub Clear()
   End Sub 'Clear

End Class 'MyCollection

Public Class SamplesNameObjectCollectionBase   

   Public Shared Sub Main()

      ' Creates and initializes a new MyCollection that is read-only. 
      Dim d As New ListDictionary()
      d.Add("red", "apple")
      d.Add("yellow", "banana")
      d.Add("green", "pear")
      Dim myROCol As New MyCollection(d, True)

      ' Tries to add a new item. 
         myROCol.Add("blue", "sky")
      Catch e As NotSupportedException
      End Try 

      ' Displays the keys and values of the MyCollection.
      Console.WriteLine("Read-Only Collection:")

      ' Creates and initializes an empty MyCollection that is writable. 
      Dim myRWCol As 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):")

      ' Changes the value of one element.
      myRWCol("orange") = "grapefruit"
      Console.WriteLine("Writable Collection (after changing one value):")

      ' Removes one item from the collection.
      Console.WriteLine("Writable Collection (after removing one value):")

      ' Removes all elements from the collection.
      Console.WriteLine("Writable Collection (after clearing the collection):")

   End Sub 'Main

   ' Prints the indexes, keys, and values. 
   Public Shared Sub PrintKeysAndValues(myCol As MyCollection)
      Dim i As Integer 
      For i = 0 To myCol.Count - 1
         Console.WriteLine("[{0}] : {1}, {2}", i, myCol(i).Key, myCol(i).Value)
      Next i
   End Sub 'PrintKeysAndValues

   ' Prints the keys and values using AllKeys. 
   Public Shared Sub PrintKeysAndValues2(myCol As MyCollection)
      Dim s As String 
      For Each s In  myCol.AllKeys
         Console.WriteLine("{0}, {1}", s, myCol(s))
      Next s
   End Sub 'PrintKeysAndValues2

End Class 'SamplesNameObjectCollectionBase

'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):

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.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0