System.Collections.Specialized.NameObjectCollectionBase.BaseHasKeys 方法

方法描述

获取一个值,通过该值指示 NameObjectCollectionBase 实例是否包含键不为 null 的项。

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

protected bool BaseHasKeys()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.Boolean 如果 NameObjectCollectionBase 实例包含键不为 null 的项,则为 true;否则为 false。

提示和注释

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

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

下面的代码示例使用 BaseHasKeys 来确定集合是否包含不是 null 的键。

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

   // Creates an empty collection.
   public MyCollection()  {
   }

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

   // Gets a value indicating whether the collection contains keys that are not a null reference.
   public Boolean HasKeys  {
      get  {
         return( this.BaseHasKeys() );
      }
   }

}

public class SamplesNameObjectCollectionBase  {

   public static void Main()  {

      // Creates an empty MyCollection instance.
      MyCollection myCol = new MyCollection();
      Console.WriteLine( "Initial state of the collection (Count = {0}):", myCol.Count );
      PrintKeysAndValues( myCol );
      Console.WriteLine( "HasKeys? {0}", myCol.HasKeys );

      Console.WriteLine();

      // Adds an item to the collection.
      myCol.Add( "blue", "sky" );
      Console.WriteLine( "Initial state of the collection (Count = {0}):", myCol.Count );
      PrintKeysAndValues( myCol );
      Console.WriteLine( "HasKeys? {0}", myCol.HasKeys );

   }

   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 = 0):
HasKeys? False

Initial state of the collection (Count = 1):
[0] : blue, sky
HasKeys? True

*/

异常

异常 异常描述

命名空间

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