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

C#のSortedDictionary.Valuesプロパティの使い方を徹底解説

C#のSortedDictionary<TKey, TValue>.Valuesプロパティは、SortedDictionaryに格納されているすべての値(Value)を含むコレクションを取得するために使用されます。

構文

Valuesプロパティの構文は以下のとおりです。

public System.Collections.Generic.SortedDictionary<TKey,TValue>.ValueCollection Values { get; }

このプロパティには、次のような特徴があります。

  • 読み取り専用: getterのみが定義されており、外部から直接代入することはできません。
  • 動的なビュー: 取得したValueCollectionは元のSortedDictionaryへのライブビューであるため、ディクショナリに対する変更がそのまま反映されます。
  • キー順での列挙: 値は、対応するキーのソート順に従って列挙されます。

使用例1:基本的な使い方

それでは、実際の使用例を見てみましょう。

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(1, "Ultrabook");
      sortedDict.Add(2, "Alienware");
      sortedDict.Add(3, "Notebook");
      sortedDict.Add(4, "Connector");
      sortedDict.Add(5, "Flash Drive");
      sortedDict.Add(6, "SSD");
      sortedDict.Add(7, "HDD");
      sortedDict.Add(8, "Earphone");

      Console.WriteLine("SortedDictionaryのキーと値のペア...");
      IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

      Console.WriteLine("\nSortedDictionaryからキー7を削除しましたか? = " + sortedDict.Remove(7));

      Console.WriteLine("\nSortedDictionaryのキーと値のペア...更新後");
      demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

      SortedDictionary<int, string>.KeyCollection keyList = sortedDict.Keys;
      Console.WriteLine("\nキーの一覧");
      foreach(int i in keyList){
         Console.WriteLine(i);
      }

      SortedDictionary<int, string>.ValueCollection valList = sortedDict.Values;
      Console.WriteLine("\n値の一覧");
      foreach(string s in valList){
         Console.WriteLine(s);
      }
   }
}

実行結果

上記のプログラムを実行すると、次の出力が得られます。

SortedDictionaryのキーと値のペア...
Key = 1, Value = Ultrabook
Key = 2, Value = Alienware
Key = 3, Value = Notebook
Key = 4, Value = Connector
Key = 5, Value = Flash Drive
Key = 6, Value = SSD
Key = 7, Value = HDD
Key = 8, Value = Earphone
SortedDictionaryからキー7を削除しましたか? = True
SortedDictionaryのキーと値のペア...更新後
Key = 1, Value = Ultrabook
Key = 2, Value = Alienware
Key = 3, Value = Notebook
Key = 4, Value = Connector
Key = 5, Value = Flash Drive
Key = 6, Value = SSD
Key = 8, Value = Earphone
キーの一覧
1
2
3
4
5
6
8
値の一覧
Ultrabook
Alienware
Notebook
Connector
Flash Drive
SSD
Earphone

この例では、まず8つのキーと値のペアを登録し、Remove(7)メソッドでキー「7」の要素を削除しています。その後、KeysプロパティValuesプロパティをそれぞれ使うことで、残った要素のキー一覧と値一覧を個別に取得している点に注目してください。

使用例2:車種データでの応用例

続いて、もう一つの例を見てみましょう。

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(1, "SUV");
      sortedDict.Add(2, "MUV");
      sortedDict.Add(3, "Hatchback");
      sortedDict.Add(4, "AUV");
      sortedDict.Add(5, "Convertible");
      sortedDict.Add(6, "Crossover");
      sortedDict.Add(7, "Utility Vehicle");

      Console.WriteLine("SortedDictionaryのキーと値のペア...");
      IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

      Console.WriteLine("\nSortedDictionaryからキー2を削除しましたか? = " + sortedDict.Remove(2));

      Console.WriteLine("\nSortedDictionaryのキーと値のペア...更新後");
      demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

      SortedDictionary<int, string>.KeyCollection keyList = sortedDict.Keys;
      Console.WriteLine("\nキーの一覧");
      foreach(int i in keyList){
         Console.WriteLine(i);
      }

      SortedDictionary<int, string>.ValueCollection valList = sortedDict.Values;
      Console.WriteLine("\n値の一覧");
      foreach(string s in valList){
         Console.WriteLine(s);
      }
   }
}

実行結果

上記のプログラムを実行すると、次の出力が得られます。

SortedDictionaryのキーと値のペア...
Key = 1, Value = SUV
Key = 2, Value = MUV
Key = 3, Value = Hatchback
Key = 4, Value = AUV
Key = 5, Value = Convertible
Key = 6, Value = Crossover
Key = 7, Value = Utility Vehicle
SortedDictionaryからキー2を削除しましたか? = True
SortedDictionaryのキーと値のペア...更新後
Key = 1, Value = SUV
Key = 3, Value = Hatchback
Key = 4, Value = AUV
Key = 5, Value = Convertible
Key = 6, Value = Crossover
Key = 7, Value = Utility Vehicle
キーの一覧
1
3
4
5
6
7
値の一覧
SUV
Hatchback
AUV
Convertible
Crossover
Utility Vehicle

まとめ

SortedDictionary.Valuesプロパティを使えば、ディクショナリ内のすべての値をキーのソート順に従って簡単に取得できます。Keysプロパティと組み合わせることで、キーと値をそれぞれ独立したコレクションとして扱うことも可能です。また、取得できるValueCollectionは元のディクショナリの変更が反映される動的なビューであるため、要素の追加や削除を行うシナリオでも常に最新の状態を扱えます。SortedDictionaryを活用したデータ管理において、ぜひ覚えておきたい基本プロパティの一つです。

  1. HTML DOMのnodeValueプロパティとは?ノード値の取得・設定方法を実例付きで解説

    HTML DOMのnodeValueプロパティは、ノードの値に対応する文字列を取得または設定するために使用されます。DOM操作において、テキストノードや属性ノードの中身を扱う際に非常に便利な基本プロパティです。構文(値の取得)ノードの値を文字列として取得する場合は、以下のように記述します。Node.nodeValue取得される戻り値は、ノードの種類によって以下のように異なります。ドキュメントノードおよび要素ノード:「null」が返される属性ノード:その属性の「value」の値が返されるテキストノードおよびコメントノード:それぞれの内容(テキストやコメント文)が返される構文(値の設定)nodeV

  2. HTML DOM valueプロパティとは?属性値の取得方法とサンプルコードを解説

    HTML DOMのvalueプロパティは、要素が持つ属性(attribute)の値に対応する文字列を返すプロパティです。JavaScriptからHTML要素の属性値を読み取りたい場合に活用できます。基本構文valueプロパティの基本的な書き方は以下の通りです。elementAttribute.value対象となる属性オブジェクトに対して.valueを指定することで、その属性が保持している値を文字列として取得できます。実装例ここでは、画像タグ(<img>)のsrc属性の値をボタンクリック時に取得して画面に表示するサンプルを紹介します。<!DOCTYPE html> <