-1

簡単なクラスを書く必要があります。2つの方法が必要です:

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;
}

ただし、文字列を配列のインデックスにすることはできません(ただし、必須です)。

何か案は?ありがとう。

4

4 に答える 4

3
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>、関連するメソッドに名前がなく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);
        }
    }
}
于 2012-11-10T11:15:03.780 に答える
2

そのための辞書を簡単に使用できます。

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

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

編集:必要に応じて、文字列だけでなく他のオブジェクトにも辞書を使用できます。「値」が実際に数値である場合は、次のようにすることもできます。

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

、これにより、数値への変換を節約できる可能性があります。

于 2012-11-10T11:14:43.483 に答える
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];
}
于 2012-11-10T11:29:12.753 に答える
1

これを行うために使用できますDictionary<TKey,TValue>。として使用したい場合Vectorのクラスとして 定義することもできますDictionaryVectorDictionary

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使用できるようになります。VectorDictionary

注意Dictionary<TKey, TValue>は、キーのセットから値のセットへのマッピングを提供するジェネリッククラスです。ディクショナリへの各追加は、値とそれに関連付けられたキーで構成されます。Dictionary<TKey, TValue>クラスはハッシュテーブルとして実装されているため、キーを使用して値を取得するのは非常に高速です。

ありがとう、
これがお役に立てば幸いです:)

于 2012-11-10T11:22:07.850 に答える