8
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【C#】超初歩のDictionary

Last updated at Posted at 2022-02-08

個人(初学者)の勉強メモになります。

Dictionaryとは?

「キーと値のコレクションを表します。」

宣言

Dictionary<string, float> dictionary = new Dictionary<string, float>() { {"key1", 0}, {"key2", 1} };

追加

// 同じKeyの値の要素を追加するとこはできない
dictionary.Add("key3", 2);

削除

Key指定で削除

dictionary.Remove("key3");

全削除

dictionary.Clear();

値の取得

key名から

dictionary["keyName"];

値からkeyを取得

KeyValuePair<string, float> res = dictionary.FirstOrDefault(v => v.Value.Equals(1));

// keyだけ取得
string res = dictionary.FirstOrDefault(v => v.Value.Equals(1)).Key;

indexでのアクセス

dictionary.ElemenntAt(index).Value;

以上です。

参考

https://www.sejuku.net/blog/41326
https://www.fenet.jp/dotnet/column/language/c-sharp/8164/

8
26
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?