C#でコレクションの指定されたインデックスにある要素を削除します
コレクションの指定されたインデックスにある要素を削除するには、コードは次のとおりです-
例
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("Kevin");
col.Add("Mary");
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);
}
col.RemoveAt(3);
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 Kevin Mary Katie Barry Nathan Mark Count of elements (updated) = 8 Iterating through the collection... (updated) Andy Kevin John Mary Katie Barry Nathan Mark
例
別の例を見てみましょう-
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("Three");
col.Add("Four");
col.Add("Five");
col.Add("Six");
Console.WriteLine("Count of elements = "+ col.Count);
Console.WriteLine("Iterating through the collection...");
var enumerator = col.GetEnumerator();
while (enumerator.MoveNext()) {
Console.WriteLine(enumerator.Current);
}
col.RemoveAt(1);
col.RemoveAt(2);
col.RemoveAt(3);
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 = 6 Iterating through the collection... One Two Three Four Five Six Count of elements (updated) = 3 Iterating through the collection... (updated) One Three Five
-
HashSetにC#で指定された要素が含まれているかどうかを確認します
HashSetに指定された要素が含まれているかどうかを確認するためのコードは、次のとおりです- 例 using System; using System.Collections.Generic; public class Demo { public static void Main(){ HashSet<int> set1 = new HashSet<int>(); set1.Add(25); set1.Add(50);
-
HashSetと指定されたコレクションがC#で共通の要素を共有しているかどうかを確認します
HashSetと指定されたコレクションが共通の要素を共有しているかどうかを確認するには、C#コードは次のとおりです- 例 using System; using System.Collections.Generic; public class Demo { public static void Main(){ HashSet<int> set1 = new HashSet<int>(); set1.Add(25); se