Skip to main content
.NET Framework Class Library
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.

Inheritance Hierarchy
SystemObject
  System.Collections.SpecializedNameObjectCollectionBase
    More...

Namespace:   System.Collections.Specialized
Assembly:  System (in System.dll)
Syntax
<[%$TOPIC/ts6a60s4_en-us_VS_110_4_0_0_0_0%]> _
Public MustInherit Class NameObjectCollectionBase _
	Implements [%$TOPIC/ts6a60s4_en-us_VS_110_4_0_0_0_1%], [%$TOPIC/ts6a60s4_en-us_VS_110_4_0_0_0_2%], [%$TOPIC/ts6a60s4_en-us_VS_110_4_0_0_0_3%], [%$TOPIC/ts6a60s4_en-us_VS_110_4_0_0_0_4%]
[[%$TOPIC/ts6a60s4_en-us_VS_110_4_0_1_0_0%]]
public abstract class NameObjectCollectionBase : [%$TOPIC/ts6a60s4_en-us_VS_110_4_0_1_0_1%], 
	[%$TOPIC/ts6a60s4_en-us_VS_110_4_0_1_0_2%], [%$TOPIC/ts6a60s4_en-us_VS_110_4_0_1_0_3%], [%$TOPIC/ts6a60s4_en-us_VS_110_4_0_1_0_4%]
[[%$TOPIC/ts6a60s4_en-us_VS_110_4_0_2_0_0%]]
public ref class NameObjectCollectionBase abstract : [%$TOPIC/ts6a60s4_en-us_VS_110_4_0_2_0_1%], 
	[%$TOPIC/ts6a60s4_en-us_VS_110_4_0_2_0_2%], [%$TOPIC/ts6a60s4_en-us_VS_110_4_0_2_0_3%], [%$TOPIC/ts6a60s4_en-us_VS_110_4_0_2_0_4%]
[<[%$TOPIC/ts6a60s4_en-us_VS_110_4_0_3_0_0%]>]
[<[%$TOPIC/ts6a60s4_en-us_VS_110_4_0_3_0_1%]>]
type NameObjectCollectionBase =  
    class 
        interface [%$TOPIC/ts6a60s4_en-us_VS_110_4_0_3_0_2%] 
        interface [%$TOPIC/ts6a60s4_en-us_VS_110_4_0_3_0_3%] 
        interface [%$TOPIC/ts6a60s4_en-us_VS_110_4_0_3_0_4%] 
        interface [%$TOPIC/ts6a60s4_en-us_VS_110_4_0_3_0_5%] 
    end

The NameObjectCollectionBase type exposes the following members.

Constructors
  NameDescription
Protected method Supported by the XNA Framework NameObjectCollectionBaseInitializes a new instance of the NameObjectCollectionBase class that is empty.
Protected method Supported by the XNA Framework NameObjectCollectionBase(IEqualityComparer)Initializes a new instance of the NameObjectCollectionBase class that is empty, has the default initial capacity, and uses the specified IEqualityComparer object.
Protected method Supported by the XNA Framework NameObjectCollectionBase(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.
Protected method Supported by the XNA Framework NameObjectCollectionBase(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.
Protected method Supported by the XNA Framework NameObjectCollectionBase(Int32, IEqualityComparer)Initializes a new instance of the NameObjectCollectionBase class that is empty, has the specified initial capacity, and uses the specified IEqualityComparer object.
Protected method NameObjectCollectionBase(SerializationInfo, StreamingContext)Initializes a new instance of the NameObjectCollectionBase class that is serializable and uses the specified System.Runtime.SerializationSerializationInfo and System.Runtime.SerializationStreamingContext.
Protected method Supported by the XNA Framework NameObjectCollectionBase(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.
Top
Properties
  NameDescription
Public property Supported by the XNA Framework CountGets the number of key/value pairs contained in the NameObjectCollectionBase instance.
Protected property Supported by the XNA Framework IsReadOnlyGets or sets a value indicating whether the NameObjectCollectionBase instance is read-only.
Public property Supported by the XNA Framework KeysGets a NameObjectCollectionBaseKeysCollection instance that contains all the keys in the NameObjectCollectionBase instance.
Top
Methods
  NameDescription
Protected method Supported by the XNA Framework BaseAddAdds an entry with the specified key and value into the NameObjectCollectionBase instance.
Protected method Supported by the XNA Framework BaseClearRemoves all entries from the NameObjectCollectionBase instance.
Protected method Supported by the XNA Framework BaseGet(Int32)Gets the value of the entry at the specified index of the NameObjectCollectionBase instance.
Protected method Supported by the XNA Framework BaseGet(String)Gets the value of the first entry with the specified key from the NameObjectCollectionBase instance.
Protected method Supported by the XNA Framework BaseGetAllKeysReturns a String array that contains all the keys in the NameObjectCollectionBase instance.
Protected method BaseGetAllValuesReturns an Object array that contains all the values in the NameObjectCollectionBase instance.
Protected method BaseGetAllValues(Type)Returns an array of the specified type that contains all the values in the NameObjectCollectionBase instance.
Protected method Supported by the XNA Framework BaseGetKeyGets the key of the entry at the specified index of the NameObjectCollectionBase instance.
Protected method Supported by the XNA Framework BaseHasKeysGets a value indicating whether the NameObjectCollectionBase instance contains entries whose keys are not .
Protected method Supported by the XNA Framework BaseRemoveRemoves the entries with the specified key from the NameObjectCollectionBase instance.
Protected method Supported by the XNA Framework BaseRemoveAtRemoves the entry at the specified index of the NameObjectCollectionBase instance.
Protected method Supported by the XNA Framework BaseSet(Int32, Object)Sets the value of the entry at the specified index of the NameObjectCollectionBase instance.
Protected method Supported by the XNA Framework BaseSet(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.
Public method Supported by the XNA Framework Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method Supported by the XNA Framework FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by the XNA Framework GetEnumeratorReturns an enumerator that iterates through the NameObjectCollectionBase.
Public method Supported by the XNA Framework GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public method GetObjectDataImplements the ISerializable interface and returns the data needed to serialize the NameObjectCollectionBase instance.
Public method Supported by the XNA Framework GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected method Supported by the XNA Framework MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public method OnDeserializationImplements the ISerializable interface and raises the deserialization event when the deserialization is complete.
Public method Supported by the XNA Framework ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Extension Methods
  NameDescription
Public Extension Method AsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension Method AsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension Method Supported by the XNA Framework CastTResultCasts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension Method Supported by the XNA Framework OfTypeTResultFilters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implemetation Private method Supported by the XNA Framework ICollectionCopyToCopies the entire NameObjectCollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.
Explicit interface implemetation Private property Supported by the XNA Framework ICollectionIsSynchronizedGets a value indicating whether access to the NameObjectCollectionBase object is synchronized (thread safe).
Explicit interface implemetation Private property Supported by the XNA Framework ICollectionSyncRootGets an object that can be used to synchronize access to the NameObjectCollectionBase object.
Top
Remarks

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 CultureInfoInvariantCulture 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.

is allowed as a key or as a value.

Caution noteCaution

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

Examples

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
      Get
            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 
      Get 
         Return Me.BaseGet(key)
      End Get 
      Set 
         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()
      Get 
         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
      Get 
         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()
      Get 
         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 
      Get 
         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)
      Me.BaseRemove(key)
   End Sub 'Remove

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

   ' Clears all the elements in the collection. 
   Public Sub Clear()
      Me.BaseClear()
   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. 
      Try
         myROCol.Add("blue", "sky")
      Catch e As NotSupportedException
         Console.WriteLine(e.ToString())
      End Try 

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

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

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

*/
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;

public ref class MyCollection : public NameObjectCollectionBase  {

private:
   DictionaryEntry^ _de;

   // Creates an empty collection. 
public:
   MyCollection()  {
      _de = gcnew DictionaryEntry();
   }

   // Adds elements from an IDictionary into the new collection.
   MyCollection( IDictionary^ d, Boolean bReadOnly )  {

      _de = gcnew DictionaryEntry();

      for each ( DictionaryEntry^ de in d )  {
         this->BaseAdd( (String^) de->Key, de->Value );
      }
      this->IsReadOnly = bReadOnly;
   }

   // Gets a key-and-value pair (DictionaryEntry) using an index. 
   property DictionaryEntry^ default[ int ]  {
      DictionaryEntry^ get(int index)  {
         _de->Key = this->BaseGetKey(index);
         _de->Value = this->BaseGet(index);
         return( _de );
      }
   }

   // Gets or sets the value associated with the specified key. 
   property Object^ default[ String^ ]  {
      Object^ get(String^ key)  {
         return( this->BaseGet( key ) );
      }
      void set( String^ key, Object^ value )  {
         this->BaseSet( key, value );
      }
   }

   // Gets a String array that contains all the keys in the collection. 
   property array<String^>^ AllKeys  {
      array<String^>^ get()  {
         return( (array<String^>^)this->BaseGetAllKeys() );
      }
   }

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

   // Gets a String array that contains all the values in the collection. 
   property array<String^>^ AllStringValues  {
      array<String^>^ get()  {
         return( (array<String^>^) this->BaseGetAllValues(  String ::typeid ));
      }
   }

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

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

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

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

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

public ref class SamplesNameObjectCollectionBase  {

public:
   static void Main()  {
      // Creates and initializes a new MyCollection that is read-only.
      IDictionary^ d = gcnew ListDictionary();
      d->Add( "red", "apple" );
      d->Add( "yellow", "banana" );
      d->Add( "green", "pear" );
      MyCollection^ myROCol = gcnew 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 = gcnew 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. 
   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. 
   static void PrintKeysAndValues2( MyCollection^ myCol )  {
      for each ( String^ s in myCol->AllKeys )  {
         Console::WriteLine( "{0}, {1}", s, myCol[s] );
      }
   }
};

int main()
{
    SamplesNameObjectCollectionBase::Main();
}

/*
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):

*/
Version Information

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Thread Safety

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.