Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

NameObjectCollectionBase::BaseAdd Method

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

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

void BaseAdd(
	String^ name, 
	Object^ value


Type: System::String

The String key of the entry to add. The key can be nullptr.

Type: System::Object

The Object value of the entry to add. The value can be nullptr.


The collection is read-only.

If Count already equals the capacity, the capacity of the NameObjectCollectionBase is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.

If Count is less than the capacity, this method is an O(1) operation. If the capacity needs to be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.

The following code example uses BaseAdd to create a new NameObjectCollectionBase with elements from an IDictionary.

#using <system.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
public ref class MyCollection: public NameObjectCollectionBase
   DictionaryEntry _de;


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

   // Adds elements from an IDictionary* into the new collection.
   MyCollection( IDictionary^ d )
      IEnumerator^ myEnum = d->GetEnumerator();
      while ( myEnum->MoveNext() )
         DictionaryEntry^ de = safe_cast<DictionaryEntry^>(myEnum->Current);
         this->BaseAdd( safe_cast<String^>(de->Key), de->Value );

int main()
   // Creates and initializes a new MyCollection instance.
   IDictionary^ d = gcnew ListDictionary;
   d->Add( "red", "apple" );
   d->Add( "yellow", "banana" );
   d->Add( "green", "pear" );
   MyCollection^ myCol = gcnew MyCollection( d );

   // Displays the keys and values of the MyCollection instance. 
   for ( int i = 0; i < myCol->Count; i++ )
      Console::WriteLine( "[{0}] : {1}, {2}", i, myCol->Item[ i ].Key, myCol->Item[ i ].Value );

This code produces the following output.

[0] : red, apple
[1] : yellow, banana
[2] : green, pear


.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
© 2015 Microsoft