System.Collections.Specialized.NameObjectCollectionBase.BaseGetKey 方法

方法描述

获取 NameObjectCollectionBase 实例的指定索引处的项键。

语法定义(C# System.Collections.Specialized.NameObjectCollectionBase.BaseGetKey 方法 的用法)

protected string BaseGetKey(
	int index
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
index System-Int32 要获取的键的从零开始的索引。
返回值 System.String 表示指定索引处的项键的 String。

提示和注释

此方法的运算复杂度是 O(1)。

System.Collections.Specialized.NameObjectCollectionBase.BaseGetKey 方法例子

下面的代码示例使用 BaseGetKey 和 BaseGet 获取特定的键和值。

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.

*/

异常

异常 异常描述
ArgumentOutOfRangeException index 在集合的有效索引范围外。

命名空间

namespace: System.Collections.Specialized

程序集: System(在 System.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。