C#のStringCollectionで指定されたインデックスにある要素を取得または設定します
StringCollectionの指定されたインデックスで要素を取得または設定するには、コードは次のとおりです-
例
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
String[] strArr = new String[] { "A", "B", "C", "D", "E", "F", "G", "H" };
Console.WriteLine("StringCollection elements...");
foreach (string str in strArr) {
Console.WriteLine(str);
}
strCol.AddRange(strArr);
Console.WriteLine("Element at 5th index = "+strCol[5]);
Console.WriteLine("Count of strings in StringCollection = " + strCol.Count);
}
} 出力
これにより、次の出力が生成されます-
StringCollection elements... A B C D E F G H Element at 5th index = F Count of strings in StringCollection = 8
例
別の例を見てみましょう-
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
String[] strArr = new String[] { "A", "B", "C", "D", "E", "F", "G", "H" };
Console.WriteLine("StringCollection elements...");
foreach (string str in strArr) {
Console.WriteLine(str);
}
strCol.AddRange(strArr);
Console.WriteLine("Element at 5th index = "+strCol[5]);
strCol[5]= "K";
Console.WriteLine("Element at 5th index [UPDATED] = "+strCol[5]);
}
} 出力
これにより、次の出力が生成されます-
StringCollection elements... A B C D E F G H Element at 5th index = F Element at 5th index [UPDATED] = K
-
C#でコレクションの指定されたインデックスにある要素を削除します
コレクションの指定されたインデックスにある要素を削除するには、コードは次のとおりです- 例 using System; using System.Collections.ObjectModel; public class Demo { public static void Main() { Collection<string> col = new Collection<string>(); col.Add("Andy"); &n
-
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);