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

C#のStringDictionaryでキーと値のペアの数を取得する方法

C#のStringDictionaryクラスでは、格納されているキーと値のペア(要素)の数を取得するためにCountプロパティを使用します。Countプロパティは、コレクション内に現在存在する要素の総数をint型の値として返します。

ここでは、実際のコード例を通じて、StringDictionaryの要素数を取得する方法を詳しく見ていきましょう。

例1:Countプロパティで要素数を取得する

まずは基本的な使い方です。StringDictionaryに複数のキーと値を追加し、その後Countプロパティで要素数を取得しています。

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      StringDictionary strDict = new StringDictionary();
      strDict.Add("1", "One");
      strDict.Add("2", "Two");
      strDict.Add("3", "Three");
      strDict.Add("4", "Four");
      Console.WriteLine("StringDictionary key-value pairs...");
      IEnumerator demoEnum = strDict.GetEnumerator();
      DictionaryEntry d;
      while (demoEnum.MoveNext()) {
         d = (DictionaryEntry)demoEnum.Current;
         Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
      }
      Console.WriteLine("Count of StringDictionary key-value pairs..."+strDict.Count);
   }
}

出力

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

StringDictionary key-value pairs...
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Count of StringDictionary key-value pairs...4

4つの要素を追加したため、Countプロパティは「4」を返していることが確認できます。

例2:複数のStringDictionaryで要素数を確認する

続いて、2つの異なるStringDictionaryオブジェクトを作成し、それぞれの要素数をCountプロパティで取得する例を見てみましょう。

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 elements...");
      foreach(DictionaryEntry de in strDict1){
         Console.WriteLine(de.Key + " " + de.Value);
      }
      Console.WriteLine("Count of StringDictionary1 key-value pairs..."+strDict1.Count);
      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);
      }
      Console.WriteLine("Count of StringDictionary2 key-value pairs..."+strDict2.Count);
   }
}

出力

このコードを実行すると、以下の出力が得られます。

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

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

ポイント解説

  • Countプロパティは、StringDictionaryに現在格納されているキーと値のペアの総数を返します。
  • 要素をAddメソッドで追加したりRemoveメソッドで削除したりすると、Countの値は自動的に更新されます。
  • 空のStringDictionaryに対してCountを呼び出した場合は0が返されます。
  • StringDictionaryのキーは大文字・小文字が区別されず、内部的に小文字へ変換されるため、出力例では「A」が「a」として表示されています。
  1. C#のStringDictionaryに特定の値が含まれているかどうかを確認する方法

    C#のStringDictionaryに特定の値(Value)が含まれているかどうかを確認するには、ContainsValue()メソッドを使用します。このメソッドは、指定した値がディクショナリ内に存在する場合は true、存在しない場合は false を返します。また、特定のキー(Key)の存在を確認したい場合は ContainsKey() メソッドを使います。以下のサンプルコードでは、これらのメソッドの基本的な使い方を紹介します。例1:ContainsValue()で値の存在を確認するまずは、複数のキーと値を持つStringDictionaryを作成し、ContainsKey()とCont

  2. 【C#】StringDictionaryに特定のキーが存在するかどうかをContainsKeyで確認する方法

    C#の StringDictionary クラスにおいて、特定のキーがコレクション内に存在するかどうかを確認するには、ContainsKey() メソッドを使用します。このメソッドは、指定したキーが存在すれば true、存在しなければ false を返します。ContainsKeyメソッドの基本構文public virtual bool ContainsKey(string key);key: 存在を確認したいキー(文字列)を指定します。戻り値: キーが見つかった場合は true、見つからない場合は false。なお、StringDictionary のキーは大文字・小文字を区別せず、内部的に