C# C中的类单向量#

C# C中的类单向量#,c#,class,vector,C#,Class,Vector,我需要写一个简单的类。 我需要两种方法: 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] = va

我需要写一个简单的类。 我需要两种方法:

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;
}
但字符串不能作为数组的索引(但必须)

有什么想法吗?
谢谢。

你可以很容易地使用字典

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

//access using:
dict["Key"];
Dictionary dict=new Dictionary();
添加(“键”、“值”);
//访问使用:
dict[“Key”];
编辑: 如果需要,还可以将字典用于其他对象,而不仅仅是字符串。如果您的“值”实际上是数字,您还可以选择:

var dict = new Dictionary<string, double>();
var dict=newdictionary();
,这可能会为您节省一些转换回数字的时间

Vector property = new Vector();  -->   var property = new Dictionary<string, string>();
property.add("key", "value");    -->   property.Add("key", "value");
property.get("key")              -->   property["key"]
术语:你心目中的东西通常被称为字典或地图;术语向量与标量相反,通常用于简单数组或值列表


p.S.:您可以创建自己的类(见下文)——尽管我无法理解为什么您拒绝使用
Dictionary
,因为相关方法没有命名为
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);
        }
    }
}
类属性映射
{
私有字典映射=新字典();
公共字符串添加(字符串键,字符串值){map.add(键,值);}
公共字符串@get(字符串键){返回映射[key];}
公共字符串此[string key]/可用于此操作。如果要将
向量
用作
字典
,还可以将
向量
定义为
字典

示例

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"
类向量:字典
{
publicstringget(stringkey)//创建一个新的void Get(stringkey),它从字典(Vector)中的特定键返回特定值
{
返回此[键];//从字典中返回键
}
public void add(string Key,string Value)//创建一个新的void add(string Key,string Value),它创建一个引用字典(Vector)中特定键的特定值
{
this.Add(Key,Value);//将键及其值添加到向量
}
}
Vector属性=new Vector();//初始化name属性的新类向量
property.add(“key”、“value”);//设置名为“key”的键及其类型为stirng的值“value”
MessageBox.Show(property.Get(“key”);//返回“value”
//MessageBox.Show(属性[“键]);//返回“值”
这将创建一个新类
Vector
,该类实现
Dictionary
,以便您能够将
Vector
用作
字典

注意:是一个泛型类,提供从一组键到一组值的映射。字典中的每个添加项都包含一个值及其关联键。使用其键检索值非常快,因为该类是作为哈希表实现的

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

使用此代码

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];
}
private Dictionary key=new Dictionary();
公共void添加(字符串键、字符串值)
{
this.keys.Add(key,value);
}
公共字符串获取(字符串键)
{
返回这个。键[key];
}

听起来像是一个
字典。
。不知道为什么称这个为“向量”。是的,但字典没有像get这样的方法。我需要它(因为它很简单)@当然是SeCorp。没有它有什么用?@SeCorp不要在你问题的顶部写
已解决的
,相反,你需要将下面的一个答案标记为答案。是的,谢谢,我不知道我可以通过那种方式获得访问权限!在很多方面,如果你不这样做,字典将失去所有的用途!是的,谢谢,我不知道我无法通过这种方式获得访问。PS.Vector(是伪代码,我想告诉你我需要什么)。补充:这有点不必要。他只使用“Vector”来描述他试图实现的目标,但实际上并不需要向量。字典本身就有他所需要的所有用途(存储要用密钥访问的内容)。
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];
}