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

C#でコレクションから最初に出現する要素だけを削除する方法

C#の Collection<T> クラスでは、Remove() メソッドを使うことで、指定したオブジェクトと一致する「最初の」要素だけを削除できます。同じ値が複数含まれている場合でも、すべてが削除されるわけではなく、先頭に最も近い1件のみが取り除かれる点に注意してください。

Remove()メソッドの基本動作

Remove(T item) メソッドは、コレクションを先頭から走査し、引数で渡した値と一致する最初の要素を見つけて削除します。戻り値はbool型で、削除に成功すれば true、該当する要素が存在しなければ false を返します。

サンプルコード1

まず、同じ名前 "Nathan" が複数含まれるコレクションから、最初の1件だけを削除してみましょう。

using System;
using System.Collections.ObjectModel;

public class Demo {
   public static void Main(){
      Collection<string> col = new Collection<string>();
      col.Add("Andy");
      col.Add("Kevin");
      col.Add("John");
      col.Add("Nathan");
      col.Add("Nathan");
      col.Add("Katie");
      col.Add("Barry");
      col.Add("Nathan");
      col.Add("Mark");

      Console.WriteLine("Count of elements = " + col.Count);
      Console.WriteLine("Iterating through the collection...");
      var enumerator = col.GetEnumerator();
      while (enumerator.MoveNext()) {
         Console.WriteLine(enumerator.Current);
      }

      // 最初の"Nathan"だけを削除
      col.Remove("Nathan");

      Console.WriteLine("Count of elements (updated) = " + col.Count);
      Console.WriteLine("Iterating through the collection... (updated)");
      enumerator = col.GetEnumerator();
      while (enumerator.MoveNext()) {
         Console.WriteLine(enumerator.Current);
      }
   }
}

実行結果

Count of elements = 9
Iterating through the collection...
Andy
Kevin
John
Nathan
Nathan
Katie
Barry
Nathan
Mark
Count of elements (updated) = 8
Iterating through the collection... (updated)
Andy
Kevin
John
Nathan
Katie
Barry
Nathan
Mark

この例では、"Nathan" が3つ含まれていますが、col.Remove("Nathan") を1回呼び出しただけで要素数は9から8に減り、残りの2つの "Nathan" はそのまま保持されています。つまり、インデックス3(4番目)の "Nathan" のみが削除されたことが確認できます。

サンプルコード2

続いて、数値を表す文字列 "Two" が複数ある場合の例を見てみましょう。

using System;
using System.Collections.ObjectModel;

public class Demo {
   public static void Main(){
      Collection<string> col = new Collection<string>();
      col.Add("One");
      col.Add("Two");
      col.Add("Two");
      col.Add("Four");
      col.Add("Five");
      col.Add("Two");
      col.Add("Six");
      col.Add("Seven");

      Console.WriteLine("Count of elements = " + col.Count);
      Console.WriteLine("Iterating through the collection...");
      var enumerator = col.GetEnumerator();
      while (enumerator.MoveNext()) {
         Console.WriteLine(enumerator.Current);
      }

      // 最初の"Two"だけを削除
      col.Remove("Two");

      Console.WriteLine("Count of elements = " + col.Count);
      Console.WriteLine("Iterating through the collection... (updated)");
      enumerator = col.GetEnumerator();
      while (enumerator.MoveNext()) {
         Console.WriteLine(enumerator.Current);
      }
   }
}

実行結果

Count of elements = 8
Iterating through the collection...
One
Two
Two
Four
Five
Two
Six
Seven
Count of elements = 7
Iterating through the collection... (updated)
One
Two
Four
Five
Two
Six
Seven

こちらの例でも同様に、"Two" が3つ含まれていますが、1回の Remove("Two") 呼び出しで削除されるのは最初の "Two"(インデックス1)だけです。要素数は8から7に減り、2番目と3番目の "Two" は残ったままになっています。

補足:一致する要素をすべて削除したい場合

条件に合う要素をすべて一括で削除したい場合は、List<T> であれば RemoveAll() メソッドが利用できます。Collection<T> の場合は、Remove()true を返す限りループで繰り返し呼び出すことで、同等の処理を実現できます。

  1. C#でStringDictionaryからすべてのエントリを削除する方法

    C#のStringDictionaryからすべてのエントリ(キーと値のペア)を一括で削除するには、Clear()メソッドを使用します。このメソッドを呼び出すと、コレクション内の全要素が削除され、Countプロパティの値は0になります。なお、StringDictionaryではキーは自動的に小文字に変換されて格納される点にも注目してください。以下に具体的なコード例を示します。例1:2つのStringDictionaryを比較してClear()を確認するusing System; using System.Collections; using System.Collections.Speciali

  2. C#でListコレクションから要素を取得する方法

    C#では、List<T>コレクションに格納した要素へ、インデックスを指定するだけで簡単にアクセスできます。本記事では、Listから要素を取得する基本的な方法を、具体的なコード例とともに解説します。 Listへの要素の追加 まず、List<int>型のコレクションを作成し、Addメソッドを使って整数の要素を追加します。 List<int> list = new List<int>(); list.Add(20); list.Add(40); list.Add(60); list.Add(80); インデックスによる要素の取得 リストから特定の