ListDictionary.Remove(Object) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定したキーを持つエントリを ListDictionary から削除します。
public:
virtual void Remove(System::Object ^ key);
public void Remove (object key);
abstract member Remove : obj -> unit
override this.Remove : obj -> unit
Public Sub Remove (key As Object)
パラメーター
- key
- Object
削除するエントリのキー。
実装
例外
key
が null
です。
例
次のコード例では、 に を追加し、 から要素を ListDictionary削除します。
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void PrintKeysAndValues( IDictionary^ myCol )
{
Console::WriteLine( " KEY VALUE" );
IEnumerator^ myEnum = myCol->GetEnumerator();
while ( myEnum->MoveNext() )
{
DictionaryEntry de = safe_cast<DictionaryEntry>(myEnum->Current);
Console::WriteLine( " {0,-25} {1}", de.Key, de.Value );
}
Console::WriteLine();
}
int main()
{
// Creates and initializes a new ListDictionary.
ListDictionary^ myCol = gcnew ListDictionary;
myCol->Add( "Braeburn Apples", "1.49" );
myCol->Add( "Fuji Apples", "1.29" );
myCol->Add( "Gala Apples", "1.49" );
myCol->Add( "Golden Delicious Apples", "1.29" );
myCol->Add( "Granny Smith Apples", "0.89" );
myCol->Add( "Red Delicious Apples", "0.99" );
// Displays the values in the ListDictionary in three different ways.
Console::WriteLine( "Initial contents of the ListDictionary:" );
PrintKeysAndValues( myCol );
// Deletes a key.
myCol->Remove( "Gala Apples" );
Console::WriteLine( "The collection contains the following elements after removing \"Gala Apples\":" );
PrintKeysAndValues( myCol );
// Clears the entire collection.
myCol->Clear();
Console::WriteLine( "The collection contains the following elements after it is cleared:" );
PrintKeysAndValues( myCol );
}
/*
This code produces the following output.
Initial contents of the ListDictionary:
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Gala Apples 1.49
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
The collection contains the following elements after removing "Gala Apples":
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
The collection contains the following elements after it is cleared:
KEY VALUE
*/
using System;
using System.Collections;
using System.Collections.Specialized;
public class SamplesListDictionary {
public static void Main() {
// Creates and initializes a new ListDictionary.
ListDictionary myCol = new ListDictionary();
myCol.Add( "Braeburn Apples", "1.49" );
myCol.Add( "Fuji Apples", "1.29" );
myCol.Add( "Gala Apples", "1.49" );
myCol.Add( "Golden Delicious Apples", "1.29" );
myCol.Add( "Granny Smith Apples", "0.89" );
myCol.Add( "Red Delicious Apples", "0.99" );
// Displays the values in the ListDictionary in three different ways.
Console.WriteLine( "Initial contents of the ListDictionary:" );
PrintKeysAndValues( myCol );
// Deletes a key.
myCol.Remove( "Gala Apples" );
Console.WriteLine( "The collection contains the following elements after removing \"Gala Apples\":" );
PrintKeysAndValues( myCol );
// Clears the entire collection.
myCol.Clear();
Console.WriteLine( "The collection contains the following elements after it is cleared:" );
PrintKeysAndValues( myCol );
}
public static void PrintKeysAndValues( IDictionary myCol ) {
Console.WriteLine( " KEY VALUE" );
foreach ( DictionaryEntry de in myCol )
Console.WriteLine( " {0,-25} {1}", de.Key, de.Value );
Console.WriteLine();
}
}
/*
This code produces the following output.
Initial contents of the ListDictionary:
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Gala Apples 1.49
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
The collection contains the following elements after removing "Gala Apples":
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
The collection contains the following elements after it is cleared:
KEY VALUE
*/
Imports System.Collections
Imports System.Collections.Specialized
Public Class SamplesListDictionary
Public Shared Sub Main()
' Creates and initializes a new ListDictionary.
Dim myCol As New ListDictionary()
myCol.Add("Braeburn Apples", "1.49")
myCol.Add("Fuji Apples", "1.29")
myCol.Add("Gala Apples", "1.49")
myCol.Add("Golden Delicious Apples", "1.29")
myCol.Add("Granny Smith Apples", "0.89")
myCol.Add("Red Delicious Apples", "0.99")
' Displays the values in the ListDictionary in three different ways.
Console.WriteLine("Initial contents of the ListDictionary:")
PrintKeysAndValues(myCol)
' Deletes a key.
myCol.Remove("Gala Apples")
Console.WriteLine("The collection contains the following elements after removing ""Gala Apples"":")
PrintKeysAndValues(myCol)
' Clears the entire collection.
myCol.Clear()
Console.WriteLine("The collection contains the following elements after it is cleared:")
PrintKeysAndValues(myCol)
End Sub
Public Shared Sub PrintKeysAndValues(myCol As IDictionary)
Console.WriteLine(" KEY VALUE")
Dim de As DictionaryEntry
For Each de In myCol
Console.WriteLine(" {0,-25} {1}", de.Key, de.Value)
Next de
Console.WriteLine()
End Sub
End Class
'This code produces the following output.
'
'Initial contents of the ListDictionary:
' KEY VALUE
' Braeburn Apples 1.49
' Fuji Apples 1.29
' Gala Apples 1.49
' Golden Delicious Apples 1.29
' Granny Smith Apples 0.89
' Red Delicious Apples 0.99
'
'The collection contains the following elements after removing "Gala Apples":
' KEY VALUE
' Braeburn Apples 1.49
' Fuji Apples 1.29
' Golden Delicious Apples 1.29
' Granny Smith Apples 0.89
' Red Delicious Apples 0.99
'
'The collection contains the following elements after it is cleared:
' KEY VALUE
'
注釈
に ListDictionary 指定したキーを持つ要素が含まれていない場合、 ListDictionary は変更されません。 例外はスローされません。
このメソッドは O(n
) 操作です。n
は Count です。
適用対象
こちらもご覧ください
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET