LoginSignup
0
0

More than 5 years have passed since last update.

csharp > Dictionary > TryGetValue() > 該当するキーがない時

Last updated at Posted at 2015-07-30

https://unity3d.com/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries?playlist=17117
のDictionary使用例において TryGetValue()というのが紹介されていた。

関連して調べると以下のサイトを見つけた。
http://blog.basyura.org/entry/20120815/p1

Dictionary でインデクサを使ってアクセスした際に該当するキーが無いと、KeyNotFoundException がスローされる。
例外が発生しないようにするためには、Dictionary#ContainsKey を使ってチェックすることになる。
そんなの効率悪いよーって人のために Dictionary#TryGetValue がある(らしい)。

今後必要になった時にまた調べようかと思う。

0
0
0

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
0
0