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

C#のStringCollectionクラス


StringCollectionクラスは、文字列のコレクションを表します。 StringCollectionクラスのプロパティは次のとおりです-

Sr.no プロパティと説明
1 カウント
OrderedDictionaryコレクションに含まれるキーと値のペアの数を取得します。
2 IsReadOnly
StringCollectionが読み取り専用かどうかを示す値を取得します。
3 IsSynchronized
StringCollectionへのアクセスが同期されている(スレッドセーフ)かどうかを示す値を取得します。
4 アイテム[Int32]
指定されたインデックスで要素を取得または設定します。
5 SyncRoot
StringCollectionへのアクセスを同期するために使用できるオブジェクトを取得します。

StringCollectionクラスのメソッドは次のとおりです-

Sr.no メソッドと説明
1 Add(String)
StringCollectionの最後に文字列を追加します。
2 AddRange(String [])
文字列配列の要素をStringCollectionの最後にコピーします。
3 Clear()
StringCollectionからすべての文字列を削除します。
4 contains(String)
指定された文字列がStringCollectionにあるかどうかを判別します。
5 CopyTo(String []、Int32)
StringCollection値全体を、ターゲット配列の指定されたインデックスから開始して、文字列の1次元配列にコピーします。
6 Equals(Object)
指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判別します。(オブジェクトから継承)
7 GetEnumerator()
StringCollectionを反復処理するStringEnumeratorを返します。

いくつかの例を見てみましょう

2つのStringCollectionオブジェクトが等しいかどうかを確認するためのコードは、次のとおりです-

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringCollection strCol1 = new StringCollection();
      strCol1.Add("Accessories");
      strCol1.Add("Books");
      strCol1.Add("Electronics");
      Console.WriteLine("StringCollection1 elements...");
      foreach (string res in strCol1) {
         Console.WriteLine(res);
      }
      StringCollection strCol2 = new StringCollection();
      strCol2.Add("Accessories");
      strCol2.Add("Books");
      strCol2.Add("Electronics");
      Console.WriteLine("StringCollection2 elements...");
      foreach (string res in strCol1) {
         Console.WriteLine(res);
      }
      Console.WriteLine("Both the String Collections are equal? = "+strCol1.Equals(strCol2));
   }
}

出力

これにより、次の出力が生成されます-

StringCollection1 elements...
Accessories
Books
Electronics
StringCollection2 elements...
Accessories
Books
Electronics
Both the String Collections are equal? = False

指定された文字列がStringCollectionにあるかどうかを確認するには、コードは次のとおりです-

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringCollection stringCol = new StringCollection();
      String[] arr = new String[] { "100", "200", "300", "400", "500" };
      Console.WriteLine("Array elements...");
      foreach (string res in arr) {
         Console.WriteLine(res);
      }
      stringCol.AddRange(arr);
      Console.WriteLine("Does the specified string is in the StringCollection? = "+stringCol.Contains("800"));
   }
}

出力

これにより、次の出力が生成されます-

Array elements...
100
200
300
400
500
Does the specified string is in the StringCollection? = False

  1. 指定された文字列がC#のStringCollectionにあるかどうかを確認します

    指定された文字列がStringCollectionにあるかどうかを確認するには、コードは次のとおりです- 例 using System; using System.Collections.Specialized; public class Demo {    public static void Main() {       StringCollection stringCol = new StringCollection();       String[] arr = new String[] { "1

  2. C#のコンソールクラス

    C#のConsoleクラスは、コンソールアプリケーションの標準の入力、出力、およびエラーストリームを表すために使用されます。 C#のコンソールクラスプロパティの例をいくつか見てみましょう- Console.CursorLeftプロパティ C#でコンソールのCursorLeftを変更するには、Console.CursorLeftプロパティを使用します。 例 例を見てみましょう- using System; class Demo {    public static void Main (string[] args) {       Cons