NameObjectCollectionBase.BaseGet Method

Definition

Gets the value of the specified entry from the NameObjectCollectionBase instance.

Overloads

BaseGet(Int32)

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

BaseGet(String)

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

Examples

The following code example uses BaseGetKey and BaseGet to get specific keys and values.

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

public class MyCollection : NameObjectCollectionBase  {

   private DictionaryEntry _de = new DictionaryEntry();

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

   // 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 );
      }
   }

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

public class SamplesNameObjectCollectionBase  {

   public static void Main()  {

      // Creates and initializes a new MyCollection instance.
      IDictionary d = new ListDictionary();
      d.Add( "red", "apple" );
      d.Add( "yellow", "banana" );
      d.Add( "green", "pear" );
      MyCollection myCol = new MyCollection( d );
      Console.WriteLine( "Initial state of the collection (Count = {0}):", myCol.Count );
      PrintKeysAndValues( myCol );

      // Gets specific keys and values.
      Console.WriteLine( "The key at index 0 is {0}.", myCol[0].Key );
      Console.WriteLine( "The value at index 0 is {0}.", myCol[0].Value );
      Console.WriteLine( "The value associated with the key \"green\" is {0}.", myCol["green"] );
   }

   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 );
      }
   }
}


/*
This code produces the following output.

Initial state of the collection (Count = 3):
[0] : red, apple
[1] : yellow, banana
[2] : green, pear
The key at index 0 is red.
The value at index 0 is apple.
The value associated with the key "green" is pear.

*/

BaseGet(Int32)

Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs

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

protected object BaseGet (int index);
protected object? BaseGet (int index);

Parameters

index
Int32

The zero-based index of the value to get.

Returns

An Object that represents the value of the entry at the specified index.

Exceptions

index is outside the valid range of indexes for the collection.

Remarks

This method is an O(1) operation.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

BaseGet(String)

Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs
Source:
NameObjectCollectionBase.cs

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

protected object BaseGet (string name);
protected object? BaseGet (string? name);

Parameters

name
String

The String key of the entry to get. The key can be null.

Returns

An Object that represents the value of the first entry with the specified key, if found; otherwise, null.

Remarks

If the collection contains multiple entries with the specified key, this method returns only the first entry. To get the values of subsequent entries with the same key, use the enumerator to iterate through the collection and compare the keys.

Caution

This method returns null in the following cases: 1) if the specified key is not found; and 2) if the specified key is found and its associated value is null. This method does not distinguish between the two cases.

This method is an O(1) operation.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0