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

C#のSortedDictionary.Countプロパティの使い方を徹底解説!要素数の取得方法とサンプルコード

C#におけるSortedDictionary.Countプロパティは、SortedDictionary<TKey, TValue>に格納されているキーと値のペア(要素)の総数を取得するために使用されます。この記事では、Countプロパティの基本的な構文から、実際のコード例までわかりやすく解説します。

構文(Syntax)

Countプロパティの構文は以下の通りです。読み取り専用のプロパティであるため、値の取得のみが可能です。

public int Count { get; }

サンプルコード例1

まず、基本的な使用例を見てみましょう。以下の例では、6つのキーと値のペアを持つSortedDictionaryを作成し、Countプロパティでその要素数を確認しています。

using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
      sortedDict.Add(100, "Mobile");
      sortedDict.Add(200, "Laptop");
      sortedDict.Add(300, "Desktop");
      sortedDict.Add(400, "Speakers");
      sortedDict.Add(500, "Headphone");
      sortedDict.Add(600, "Earphone");
      Console.WriteLine("SortedDictionary key-value pairs...");
      IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
      Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      Console.WriteLine("Count of SortedDictionary key-value pairs = "+sortedDict.Count);
      Console.WriteLine("\nThe SortedDictionary has the key 200? = "+sortedDict.ContainsKey(200));
      sortedDict.Clear();
      Console.WriteLine("Count of SortedDictionary key-value pairs = "+sortedDict.Count);
   }
}

実行結果

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

SortedDictionary key-value pairs...
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 400, Value = Speakers
Key = 500, Value = Headphone
Key = 600, Value = Earphone
Count of SortedDictionary key-value pairs = 6
The SortedDictionary has the key 200? = True
Count of SortedDictionary key-value pairs = 0

この例では、最初に6つの要素を追加したためCountは「6」を返します。また、ContainsKey()メソッドでキー「200」の存在確認を行い、その後Clear()メソッドですべての要素を削除したことで、Countが「0」になっていることがわかります。

サンプルコード例2:要素追加後のCount変化

次に、要素を追加した際にCountプロパティの値がどのように変化するかを見てみましょう。

using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
      sortedDict.Add(100, "Inspiron");
      sortedDict.Add(200, "Alienware");
      sortedDict.Add(300, "Projectors");
      sortedDict.Add(400, "XPS");
      Console.WriteLine("SortedDictionary key-value pairs...");
      IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
      Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      Console.WriteLine("Count of SortedDictionary key-value pairs = "+sortedDict.Count);
      sortedDict.Add(800, "Notebook");
      sortedDict.Add(10000, "Bluetooth Speaker");
      Console.WriteLine("\nSortedDictionary key-value pairs...UPDATED");
      demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
      Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      Console.WriteLine("Count of SortedDictionary key-value pairs (UPDATED) = "+sortedDict.Count);
      sortedDict.Clear();
      Console.WriteLine("\nCount of SortedDictionary key-value pairs (UPDATED) = "+sortedDict.Count);
   }
}

実行結果

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

SortedDictionary key-value pairs...
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 300, Value = Projectors
Key = 400, Value = XPS
Count of SortedDictionary key-value pairs = 4
SortedDictionary key-value pairs...UPDATED
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 300, Value = Projectors
Key = 400, Value = XPS
Key = 800, Value = Notebook
Key = 10000, Value = Bluetooth Speaker
Count of SortedDictionary key-value pairs (UPDATED) = 6
Count of SortedDictionary key-value pairs (UPDATED) = 0

この例では、最初に4つの要素を登録した状態でCountは「4」ですが、さらに2つの要素を追加した後は「6」に更新されています。最後にClear()メソッドで全要素を削除すると、Countは「0」に戻ります。

まとめ

SortedDictionary.Countプロパティは、コレクション内の現在の要素数を動的に把握できる便利な機能です。要素の追加や削除に応じて自動的に更新されるため、ループ処理の条件判定やコレクションの空チェックなど、さまざまな場面で活用できます。なお、Countプロパティの取得はO(1)の計算量で行われるため、パフォーマンスへの影響を気にせず利用できます。

  1. HTML DOM KeyboardEventのkeyプロパティとは?押されたキーを判定する方法を解説

    HTML DOM の KeyboardEvent オブジェクトが持つ key プロパティは、ユーザーが押したキーに対応する「キー識別子」を文字列として返します。このプロパティを活用することで、どのキーが入力されたのかを簡単に判別でき、フォームのバリデーションやショートカット機能の実装などに役立ちます。基本情報対象イベント: keydown / keyup / keypress などのキーボードイベント戻り値: 押されたキーに対応する文字列(例:「a」「Enter」「Shift」など)読み取り専用: 値の設定はできません構文最後に入力されたキーの識別子を返す構文は以下のとおりです。event.k

  2. 【C#】Console.KeyAvailableプロパティの使い方とサンプルコード

    Console.KeyAvailableプロパティとはC#のConsole.KeyAvailableプロパティは、標準入力ストリームにキー入力が利用可能かどうかを示す値を取得するためのプロパティです。戻り値はbool型で、キー入力があればtrue、なければfalseを返します。このプロパティは読み取り専用(getのみ)であり、Console.ReadKey()メソッドと組み合わせることで、処理をブロックすることなくキー入力の有無を確認しながらループ処理を実装できます。構文public static bool KeyAvailable { get; }使用例1:キー入力を検出して表示する以下は、