C#のDictionary.Keysプロパティとは?使い方とサンプルコードを解説
C#におけるDictionary.Keysプロパティは、Dictionary(辞書)オブジェクトに格納されたすべてのキーを取得するためのプロパティです。戻り値として KeyCollection 型のコレクションを返すため、foreachループなどを使って各キーを簡単に列挙できます。
構文
Dictionary.Keysプロパティの構文は以下の通りです。
public System.Collections.Generic.Dictionary<TKey, TValue>.KeyCollection Keys { get; }このプロパティは読み取り専用であり、取得したキーのコレクション自体に要素を追加したり削除したりすることはできません。また、返されるコレクションは元のDictionaryと連動しているため、Dictionaryの内容が変更されると、Keysプロパティの結果にもその変更が反映されます。
使用例1:すべてのキーを取得して表示する
まずは基本的な使い方を見てみましょう。以下の例では、5つのキーと値のペアを持つDictionaryを作成し、Keysプロパティを使ってすべてのキーをコンソールに表示しています。
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, string> dict =
new Dictionary<string, string>();
dict.Add("One", "Kagido");
dict.Add("Two", "Ngidi");
dict.Add("Three", "Devillers");
dict.Add("Four", "Smith");
dict.Add("Five", "Warner");
Console.WriteLine("Count of elements = "+dict.Count);
Console.WriteLine("\nKey/value pairs...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
Console.Write("\nAll the keys..\n");
Dictionary<string, string>.KeyCollection allKeys =
dict.Keys;
foreach(string str in allKeys){
Console.WriteLine("Key = {0}", str);
}
}
}出力結果
上記のプログラムを実行すると、次のような出力が得られます。
Count of elements = 5 Key/value pairs... Key = One, Value = Kagido Key = Two, Value = Ngidi Key = Three, Value = Devillers Key = Four, Value = Smith Key = Five, Value = Warner All the keys.. Key = One Key = Two Key = Three Key = Four Key = Five
使用例2:値の更新とキーの一覧取得を組み合わせる
続いて、特定のキーに関連付けられた値を更新したうえで、Keysプロパティによってキーの一覧を取得する例を見てみましょう。インデックス(キー)を指定して値を読み書きする方法も合わせて確認できます。
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, string> dict =
new Dictionary<string, string>();
dict.Add("One", "Chris");
dict.Add("Two", "Steve");
dict.Add("Three", "Messi");
dict.Add("Four", "Ryan");
dict.Add("Five", "Nathan");
Console.WriteLine("Count of elements = "+dict.Count);
Console.WriteLine("\nKey/value pairs...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
Console.WriteLine("Value for key three = "+dict["Three"]);
dict["Three"] = "Katie";
Console.WriteLine("Updated value associated with key Three...");
Console.WriteLine(dict["Three"]);
Console.Write("\nAll the keys..\n");
Dictionary<string, string>.KeyCollection allKeys = dict.Keys;
foreach(string str in allKeys){
Console.WriteLine("Key = {0}", str);
}
}
}出力結果
上記のプログラムを実行すると、次のような出力が得られます。
Count of elements = 5 Key/value pairs... Key = One, Value = Chris Key = Two, Value = Steve Key = Three, Value = Messi Key = Four, Value = Ryan Key = Five, Value = Nathan Value for key three = Messi Updated value associated with key Three... Katie All the keys.. Key = One Key = Two Key = Three Key = Four Key = Five
まとめ
Dictionary.Keysプロパティを使えば、Dictionary内のすべてのキーを簡単に取得できます。キーを指定した値の更新処理と組み合わせることで、データの参照・変更・一覧表示を柔軟に行えるようになります。なお、キーではなく値の一覧を取得したい場合は、同じく読み取り専用のValuesプロパティを使用します。
-
C#のSortedDictionary.Countプロパティの使い方を徹底解説!要素数の取得方法とサンプルコード
C#におけるSortedDictionary.Countプロパティは、SortedDictionary<TKey, TValue>に格納されているキーと値のペア(要素)の総数を取得するために使用されます。この記事では、Countプロパティの基本的な構文から、実際のコード例までわかりやすく解説します。構文(Syntax)Countプロパティの構文は以下の通りです。読み取り専用のプロパティであるため、値の取得のみが可能です。public int Count { get; }サンプルコード例1まず、基本的な使用例を見てみましょう。以下の例では、6つのキーと値のペアを持つSortedDic
-
【C#】Console.KeyAvailableプロパティの使い方とサンプルコード
Console.KeyAvailableプロパティとはC#のConsole.KeyAvailableプロパティは、標準入力ストリームにキー入力が利用可能かどうかを示す値を取得するためのプロパティです。戻り値はbool型で、キー入力があればtrue、なければfalseを返します。このプロパティは読み取り専用(getのみ)であり、Console.ReadKey()メソッドと組み合わせることで、処理をブロックすることなくキー入力の有無を確認しながらループ処理を実装できます。構文public static bool KeyAvailable { get; }使用例1:キー入力を検出して表示する以下は、