Vector property = new Vector(); --> var property = new Dictionary<string, string>();
property.add("key", "value"); --> property.Add("key", "value");
property.get("key") --> property["key"]
例外処理:
キーが辞書に見つからない場合、最後の1つは例外をスローする可能性があります。決して投げない別の方法は次のとおりです。
string value;
bool keyFound = property.TryGetValue("key", out value);
用語:あなたが考えているものは、通常、辞書または地図と呼ばれます。スカラーの反対であるベクトルという用語は、通常、単純な配列または値のリスト用に予約されています。
PS:独自のクラスを作成できます(以下を参照)—ただしDictionary<TKey,TValue>
、関連するメソッドに名前がなくadd
、get
私を超えているという理由だけで拒否するのはなぜですか。
class PropertyMap
{
private Dictionary<string, string> map = new Dictionary<string, string>();
public string add(string key, string value) { map.Add(key, value); }
public string @get(string key) { return map[key]; }
public string this[string key] // <-- indexer allows you to access by string
{
get
{
return @get(key);
}
set
{
add(key, value);
}
}
}