C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#のStringDictionaryから指定したキーのエントリを削除する方法

C# の StringDictionary から指定したキーのエントリ(キーと値のペア)を削除するには、Remove() メソッドを使用します。削除したいキーを引数として渡すだけで、該当する要素がコレクションから取り除かれます。なお、指定したキーが存在しない場合でも例外はスローされず、ディクショナリは元のまま変化しません。

また、StringDictionary は既定でキーの大文字と小文字を区別しないという特徴があります。そのため、「A」というキーで値を追加しても、列挙時には小文字の「a」として表示される点に注意してください。

サンプルコード 1:単一のキーを削除する

以下の例では、7 組のキーと値を持つ StringDictionary を作成し、Remove() メソッドでキー「D」のエントリを削除しています。後半では、IEnumerator を使った別の列挙方法も併せて示しています。

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "John");
      strDict1.Add("B", "Andy");
      strDict1.Add("C", "Tim");
      strDict1.Add("D", "Ryan");
      strDict1.Add("E", "Kevin");
      strDict1.Add("F", "Katie");
      strDict1.Add("G", "Brad");
      Console.WriteLine("StringDictionary1 key-value pairs...");
      foreach(DictionaryEntry de in strDict1) {
         Console.WriteLine(de.Key + " " + de.Value);
      }
      strDict1.Remove("D");
         Console.WriteLine("\nStringDictionary1 key-value pairs after removing a key...");
      foreach(DictionaryEntry de in strDict1) {
         Console.WriteLine(de.Key + " " + de.Value);
      }
      StringDictionary strDict2 = new StringDictionary();
      strDict2.Add("1", "A");
      strDict2.Add("2", "B");
      strDict2.Add("3", "C");
      strDict2.Add("4", "D");
      strDict2.Add("5", "E");
      Console.WriteLine("\nStringDictionary2 key-value pairs...");
      IEnumerator demoEnum = strDict2.GetEnumerator();
      DictionaryEntry d;
      while (demoEnum.MoveNext()) {
         d = (DictionaryEntry)demoEnum.Current;
         Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
      }
   }
}

実行結果

上記のコードを実行すると、次のような出力が得られます。

StringDictionary1 key-value pairs...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad

StringDictionary1 key-value pairs after removing a key...
a John
b Andy
c Tim
e Kevin
f Katie
g Brad

StringDictionary2 key-value pairs...
Key = 1, Value = A
Key = 2, Value = B
Key = 3, Value = C
Key = 4, Value = D
Key = 5, Value = E

実行結果を見ると、Remove("D") を呼び出した後の出力から「d Ryan」の行が消えており、キー「D」のエントリだけが正しく削除されていることが確認できます。それ以外のエントリには影響していません。

サンプルコード 2:複数のキーを削除する

続いて、Remove() メソッドを複数回呼び出して、複数のエントリをまとめて削除する例を見てみましょう。

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "One");
      strDict1.Add("B", "Two");
      strDict1.Add("C", "Three");
      strDict1.Add("D", "Four");
      strDict1.Add("E", "Five");
      Console.WriteLine("StringDictionary1 key-value pairs...");
      foreach(DictionaryEntry de in strDict1) {
         Console.WriteLine(de.Key + " " + de.Value);
      }
      strDict1.Remove("B");
      strDict1.Remove("C");
      Console.WriteLine("\nStringDictionary1 key-value pairs after removing a key...");
      foreach(DictionaryEntry de in strDict1) {
         Console.WriteLine(de.Key + " " + de.Value);
      }
   }
}

実行結果

上記のコードを実行すると、次のような出力が得られます。

StringDictionary1 key-value pairs...
a One
b Two
c Three
d Four
e Five

StringDictionary1 key-value pairs after removing a key...
a One
d Four
e Five

この例では、キー「B」と「C」に対応する 2 つのエントリが削除され、残りの 3 つのエントリのみが出力されています。このように Remove() メソッドを繰り返し呼び出すことで、任意の数のエントリを柔軟に削除できます。

  1. Windows Decrapifier&Debloaterを使用してWindows10から綿毛を取り除きます

    リリースの時点で、Windows10はMicrosoftの最もスリムで平均的なOSでした。その前身であるWindows7よりもさらにジッパーのように感じられ、IoTデバイスで動作するのに十分コンパクトでした。しかし、それはその時でした。今日、Windows 10は、それを台無しにするのに十分な「機能」を蓄積しています。 多くのユーザーにとって、これらの「機能」は実際には役に立たないため、引用符を使用します。あなたも、最近Windows 10の速度が低下していることに気づき、Microsoftにその感想を伝えてもかまわない場合は(別名:テレメトリ機能を使用する)、OSをデクレイプする時期かもし

  2. Python Pandas入門:CategoricalIndexから指定したカテゴリを削除する方法

    CategoricalIndexから特定のカテゴリを削除するには、Pandasのremove_categories()メソッドを使用します。このメソッドを使うと、指定したカテゴリがインデックスから取り除かれ、それらのカテゴリに属していた値は自動的にNaN(欠損値)に置き換えられます。必要なライブラリのインポートまず、必要なライブラリをインポートします。import pandas as pdCategoricalIndexの作成「categories」パラメータを使用してカテゴリカルデータのカテゴリを設定し、「ordered」パラメータを使用して順序付きのカテゴリカルとして扱います。catInd