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

C#のSortedDictionary.Item[]プロパティとは?使い方とサンプルコードを解説

C#のSortedDictionary.Item[]プロパティは、指定したキーに関連付けられた値を取得または設定するために使用されます。インデクサとして機能するため、配列のようにキーを指定して要素へアクセスできるのが特徴です。

構文

Item[]プロパティの構文は以下の通りです。

public TValue this[TKey k] { get; set; }

ここで、k は取得または設定対象となる値のキーを表します。getでは指定したキーに対応する値を読み取り、setではそのキーに新しい値を割り当てることができます。

使用例1:値の取得

まず、Item[]プロパティを使って特定のキーの値を取得する例を見てみましょう。

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, "One");
      sortedDict.Add(2, "Two");
      sortedDict.Add(3, "Three");
      sortedDict.Add(4, "Four");
      sortedDict.Add(5, "Five");
      sortedDict.Add(6, "Six");

      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(7, "Seven");
      sortedDict.Add(8, "Eight");

      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);

      // Item[]プロパティでキー5の値を取得
      Console.WriteLine("Value in the SortedDictionary key-value pair key five = " + sortedDict[5]);

      sortedDict.Clear();
      Console.WriteLine("\nCount of SortedDictionary key-value pairs (UPDATED) = " + sortedDict.Count);
   }
}

実行結果

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

Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six
Count of SortedDictionary key-value pairs = 6
SortedDictionary key-value pairs...UPDATED
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six
Key = 7, Value = Seven
Key = 8, Value = Eight
Count of SortedDictionary key-value pairs (UPDATED) = 8
Value in the SortedDictionary key-value pair key five = Five
Count of SortedDictionary key-value pairs (UPDATED) = 0

この例では、sortedDict[5] のようにキーを指定することで、キー5に関連付けられた値「Five」を取得できていることが確認できます。

使用例2:値の設定(更新)

続いて、Item[]プロパティを使って既存のキーの値を更新する例を見てみましょう。

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

      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(7, "Seven");
      sortedDict.Add(8, "Eight");

      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);

      // Item[]プロパティでキー5の値を取得
      Console.WriteLine("Value in the SortedDictionary key-value pair key five = " + sortedDict[5]);

      // Item[]プロパティでキー5の値を更新
      sortedDict[5] = "Crossover";
      Console.WriteLine("Value in the SortedDictionary key-value pair key five (updated) = " + sortedDict[5]);

      sortedDict.Clear();
      Console.WriteLine("\nCount of SortedDictionary key-value pairs (UPDATED) = " + sortedDict.Count);
   }
}

実行結果

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

SortedDictionary key-value pairs...
Key = 1, Value = SUV
Key = 2, Value = MUV
Key = 3, Value = Utility Vehicle
Key = 4, Value = AUV
Key = 5, Value = Hatchback
Key = 6, Value = Convertible
Count of SortedDictionary key-value pairs = 6
SortedDictionary key-value pairs...UPDATED
Key = 1, Value = SUV
Key = 2, Value = MUV
Key = 3, Value = Utility Vehicle
Key = 4, Value = AUV
Key = 5, Value = Hatchback
Key = 6, Value = Convertible
Key = 7, Value = Seven
Key = 8, Value = Eight
Count of SortedDictionary key-value pairs (UPDATED) = 8
Value in the SortedDictionary key-value pair key five = Hatchback
Value in the SortedDictionary key-value pair key five (updated) = Crossover
Count of SortedDictionary key-value pairs (UPDATED) = 0

実行結果から、sortedDict[5] = "Crossover"; という代入によって、キー5の値が「Hatchback」から「Crossover」に更新されたことがわかります。

まとめ

SortedDictionary.Item[]プロパティを使うことで、キーを指定して値の取得・設定を直感的に行えます。なお、存在しないキーを取得しようとした場合は KeyNotFoundException がスローされるため、事前に ContainsKey() メソッドなどでキーの存在を確認しておくと安全です。また、存在しないキーに対してsetを行った場合は、新しい要素として自動的に追加される点も覚えておくと便利です。

  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> <