Help us understand the problem. What is going on with this article?

C#のDictionary【Unity, C#】

More than 3 years have passed since last update.

C#でDictionaryを使用したので、メモ。

名前空間

using System.Collections.Generic;

宣言

Dictionary<GameObject, Vector3> temp;

コンストラクタ

Dictionary<string, string> dic = new Dictionary<string, string> ();

初期値を設定する場合

Dictionary<string, string> dic = new Dictionary<string, string> () {
        {"key0", "value0"},
        {"key1", "value1"}
    };

要素の追加

dic.Add ("key2", "value2");
dic.Add ("key3", "value3");

要素の取り出し

Debug.Log (dic ["key0"]);

キーの列挙

foreach (string key in dic.Keys) {
    Debug.Log (key);
}

要素の列挙

foreach (string value in dic.Values) {
    Debug.Log (value);
}

キーと要素の列挙

foreach (KeyValuePair<string, string> pair in dic) {
    Debug.Log (pair.Key + " : " + pair.Value);
}

要素数

Debug.Log (dic.Count);

要素列挙の順番

Addの順番通りに出力されます。
※このサンプルがたまたまAddと同じ順番で出力されているだけなのかもしれません。

Dictionary<string, string> temp = new Dictionary<string, string> ();
for (int i = 0; i < 10; i++) {
    temp.Add ("key" + i.ToString(), "value" + i.ToString());
}

foreach (KeyValuePair<string, string> pair in temp) {
    Debug.Log(pair.Key + " : " + pair.Value);
}

結果
スクリーンショット 2015-08-04 14.25.37.png

要素の削除

dic.Remove ("key0");
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした