Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
How do you check to see if a key exists in a Dictionary?
Posted 17-Aug-11 17:11pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can tell if a dictionary has a key in it because it won't close properly, unless it's a really small key.
 
Honestly, you spent more time typing that message than it would have taken to just look yourself. Documentation[^] It's not just there for fun.
  Permalink  
Comments
SAKryukov at 17-Aug-11 23:03pm
   
Exactly. Also, it could be hard to tell if the dictionary is really big. My 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Just use ContainsKey Method[^] to check a key existence in the dictionary.
  Permalink  
Comments
SAKryukov at 17-Aug-11 23:08pm
   
Sure, a 5. I credited your answer in mine -- another way is TryGetValue, please see my answer.
--SA
Wonde Tadesse at 6-Dec-11 11:18am
   
Thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

In addition to what Wonde advised: you can also use System.Collections.Generic.Dictionary.TryGetValue, see http://msdn.microsoft.com/en-us/library/bb347013.aspx[^].
 
To confirm what SK Genius say, you would spent more time typing that message than it would have taken to just look yourself.
 
—SA
  Permalink  
Comments
Wonde Tadesse at 17-Aug-11 23:08pm
   
5+. Another possible solution.
SAKryukov at 17-Aug-11 23:09pm
   
Thank you, Wonde. How could you be so fast?
--SA
Wonde Tadesse at 17-Aug-11 23:23pm
   
No wonder.I'm fast like lightning.:) Just kidding.As usual I was browsing the Q/A sections and suddenly my reputation points up lifted drastically.Finally I just checked my answers what I replied ... I am here look what I got.
SAKryukov at 17-Aug-11 23:27pm
   
We're more overwhelmed by tons of bad questions these days, have less time to review each others solutions...
--SA
Wonde Tadesse at 17-Aug-11 23:35pm
   
Very True. These days I am quite busy in office. I am less active than before. But still I am checking some members solution.
SAKryukov at 18-Aug-11 11:54am
   
Me too... but I'm trying not to disappear completely.
Good to hear from you.
--SA
Mika Wendelius at 18-Aug-11 0:43am
   
Good alternative especially because the actual value is most likely needed later in the code. My 5
SAKryukov at 18-Aug-11 11:53am
   
Exactly. It depends on where to check up the presence of the key.
Thank you, Mika.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 485
1 OriginalGriff 310
2 Shweta N Mishra 244
3 Mathew Soji 225
4 BillWoodruff 200
0 OriginalGriff 6,153
1 Sergey Alexandrovich Kryukov 5,758
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,735


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 17 Aug 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100