key
&などの 2 つのプロパティを持つクラスを作成したいと考えていますvalue
。
そして、 にvalue
基づいてを提供するメソッドが 1 つ必要ですkey
。
それで、コードは何ですか?私は知ってHashtable
いますが、C#で実装する方法は? string
を鍵にできますか?
key
&などの 2 つのプロパティを持つクラスを作成したいと考えていますvalue
。
そして、 にvalue
基づいてを提供するメソッドが 1 つ必要ですkey
。
それで、コードは何ですか?私は知ってHashtable
いますが、C#で実装する方法は? string
を鍵にできますか?
Dictionary<TKey, TValue>
クラスを見てください: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx
これを実装する最良の方法は次のとおりです (格納する型の例として Int32 を使用します)。
Dictionary<String,Int32> dictionary = new Dictionary<String,Int32>
{
// this just loads up the list with
// some dummy data - notice that the
// key is a string and the value is an int
{ "one", 1 },
{ "two", 2 },
{ "three", 3 },
};
これで、次のように Dictionary<,> から値を取得できます。
dictionary["one"]; // returns 1
dictionary["two"]; // returns 2
ADictionary<string, T>
はあなたが望むすべてをします。
さらにいくつかの実装があります。セット操作用に設計されたHashSetと、簡単にシリアル化できるハッシュ テーブルであるKeyedCollectionがあります。
使用するDictionary<string, TypeOfYourVAlue>