Vector property = new Vector(); --> var property = new Dictionary<string, string>();
property.add("key", "value"); --> property.Add("key", "value");
property.get("key") --> property["key"]
异常处理: 如果该键不字典中的最后一个可能抛出异常。它不会抛出的另一种方法是:
string value;
bool keyFound = property.TryGetValue("key", out value);
术语:,你有什么想法通常被称为字典或地图;术语矢量,与标量相反,通常保留用于简单数组或值列表。
P.S:您可以创建自己的类(见下文)—但你为什么拒绝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);
}
}
}
听起来像一个'字典'。不知道你为什么称这个“矢量”。 –
CodesInChaos
是的,但字典没有像get那样的方法。我需要(因为很简单) – Sekhmet
@SeCorp当然[它](http://msdn.microsoft.com/en-us/library/9tee9ht2(v = vs.90).aspx)。没有它会有什么用处? – GSerg