System.Collections.Hashtable.Remove 方法
上一篇:system.configuration.settingsattributedictionary.OnDeserialization 方法
下一篇:System.Configuration.SettingsBase 类
方法描述
从 Hashtable 中移除带有指定键的元素。
语法定义(C# System.Collections.Hashtable.Remove 方法 的用法)
public virtual void Remove( Object key )
参数/返回值
| 参数值/返回值 | 参数类型/返回类型 | 参数描述/返回描述 |
|---|---|---|
| key | System-Object | 要移除的元素的键。 |
| 返回值 | void |
提示和注释
如果 Hashtable 不包含带有指定键的元素,则 Hashtable 保持不变。 不引发异常。
此方法的运算复杂度是 O(1)。
System.Collections.Hashtable.Remove 方法例子
下面的示例说明如何移除 Hashtable 中的元素。
using System;
using System.Collections;
public class SamplesHashtable {
public static void Main() {
// Creates and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add( "1a", "The" );
myHT.Add( "1b", "quick" );
myHT.Add( "1c", "brown" );
myHT.Add( "2a", "fox" );
myHT.Add( "2b", "jumped" );
myHT.Add( "2c", "over" );
myHT.Add( "3a", "the" );
myHT.Add( "3b", "lazy" );
myHT.Add( "3c", "dog" );
// Displays the Hashtable.
Console.WriteLine( "The Hashtable initially contains the following:" );
PrintKeysAndValues( myHT );
// Removes the element with the key "3b".
myHT.Remove( "3b" );
// Displays the current state of the Hashtable.
Console.WriteLine( "After removing \"lazy\":" );
PrintKeysAndValues( myHT );
}
public static void PrintKeysAndValues( Hashtable myHT ) {
foreach ( DictionaryEntry de in myHT )
Console.WriteLine( " {0}: {1}", de.Key, de.Value );
Console.WriteLine();
}
}
/*
This code produces the following output.
The Hashtable initially contains the following:
2c: over
3a: the
2b: jumped
3b: lazy
1b: quick
3c: dog
2a: fox
1c: brown
1a: The
After removing "lazy":
2c: over
3a: the
2b: jumped
1b: quick
3c: dog
2a: fox
1c: brown
1a: The
*/
异常
| 异常 | 异常描述 |
|---|---|
| ArgumentNullException | key 为 null。 |
| NotSupportedException |
|
版本信息
.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 系统要求。
上一篇:system.configuration.settingsattributedictionary.OnDeserialization 方法
下一篇:System.Configuration.SettingsBase 类