System.Collections.Specialized.NameObjectCollectionBase.BaseGetAllKeys 方法

方法描述

返回 String 数组,该数组包含 NameObjectCollectionBase 实例中的所有键。

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

protected string[] BaseGetAllKeys()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.String[] String 数组,该数组包含 NameObjectCollectionBase 实例中的所有键。

提示和注释

此方法的运算复杂度为 O(n),其中 n 是 Count。

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

下面的代码示例使用 BaseGetAllKeys 和 BaseGetAllValues 获取键数组或值数组。

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

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

   // 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(System.String) ) );
      }
   }

}

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

      // Displays the list of keys.
      Console.WriteLine( "The list of keys:" );
      foreach ( String s in myCol.AllKeys )  {
         Console.WriteLine( "   {0}", s );
      }

      // Displays the list of values of type Object.
      Console.WriteLine( "The list of values (Object):" );
      foreach ( Object o in myCol.AllValues )  {
         Console.WriteLine( "   {0}", o.ToString() );
      }

      // Displays the list of values of type String.
      Console.WriteLine( "The list of values (String):" );
      foreach ( String s in myCol.AllValues )  {
         Console.WriteLine( "   {0}", s );
      }

   }

   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 list of keys:
   red
   yellow
   green
The list of values (Object):
   apple
   banana
   pear
The list of values (String):
   apple
   banana
   pear

*/

异常

异常 异常描述

命名空间

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 系统要求。