2012-11-10 92 views
-1

我需要写一个简单的类。 我需要2种方法:类只是简单的矢量在C#

Vector property = new Vector(); 
property.add("key", "value"); //set value of key 
property.get("key"); //return value of key 

CSHARP是否有一个这样的类?

我试图写我自己的类

string[] keys; 

public void add(string key, string value) 
{ 
this.keys[key] = value; 
} 

但字符串不能为数组的索引(但必须)。

任何想法? 谢谢。

+8

听起来像一个'字典'。不知道你为什么称这个“矢量”。 – CodesInChaos

+0

是的,但字典没有像get那样的方法。我需要(因为很简单) – Sekhmet

+1

@SeCorp当然[它](http://msdn.microsoft.com/en-us/library/9tee9ht2(v = vs.90).aspx)。没有它会有什么用处? – GSerg

回答

1

您可以使用Dictionary<TKey,TValue>来做到这一点。您也可以定义Vector作为一类Dictionary,如果你想使用Vector作为Dictionary

class Vector : Dictionary<string,string> 
{ 
    public string Get(string Key) //Create a new void Get(string Key) which returns a particular value from a specific Key in the Dictionary (Vector) 
    { 
     return this[Key]; //Return the key from the Dictionary 
    } 
    public void add(string Key, string Value) //Create a new void Add(string Key, string Value) which creates a particular value referring to a specific Key in the Dictionary (Vector) 
    { 
     this.Add(Key, Value); //Add the key and its value to Vector 
    } 
} 

Vector property = new Vector(); //Initialize a new class Vector of name property 
property.add("key", "value"); //Sets a key of name "key" and its value "value" of type stirng 
MessageBox.Show(property.Get("key")); //Returns "value" 
//MessageBox.Show(property["key"]); //Returns "value" 

这将创建一个新的类Vector它实现Dictionary,这样你就可以能够使用Vector作为Dictionary

注意事项Dictionary<TKey, TValue>是一个泛型类,提供从一组键到一组值的映射。字典中的每个添加项都包含一个值及其关联的键。使用密钥检索值非常快,因为Dictionary<TKey, TValue>类是作为散列表实现的。

谢谢,
我希望对您有所帮助:)

+0

要添加:这是不必要的。他只用“矢量”来描述他试图完成的事情,但并不真正需要媒介。字典本身具有他需要的所有目的(存储要用键访问的内容)。 – phil13131

2

你可以很容易地使用一个字典。

Dictionary<string, string> dict = new Dictionary<string, string>(); 
dict.Add("Key", "Value"); 

//access using: 
dict["Key"]; 

编辑: 如果你愿意,你也可以用字典为其他对象不仅字符串。如果你的“价值”实际上是数字,你也可以去:

var dict = new Dictionary<string, double>(); 

,这可能会节省你一些转换回数字。

+0

是的,谢谢,我不知道我不能通过这种方式获得acces! – Sekhmet

+0

在许多方面,如果你不喜欢,字典会失去它的全部目的! – phil13131

3
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>仅仅是因为相关的方法没有被命名addget是超越我。

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); 
     } 
    } 
} 
+0

是的,谢谢,我不知道我不能通过这种方式获得acces。 PS。向量(是伪代码,我想向你展示我的内容)。 – Sekhmet

2

使用此代码...

private Dictionary<string, string> keys = new Dictionary<string, string>(); 

public void add(string key, string value) 
{ 
    this.keys.Add(key, value); 
} 

public string get(string key) 
{ 
    return this.keys[key]; 
}