Click here to Skip to main content
15,887,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have 2 string ,int Dictionaries named statsB and statsC.
I want to check if a string from StatsB is contained within statsC and if so compare the correlating integer of both.
I have no idea how to start

What I have tried:

foreach (var item in statsB)
          {
              if (statsC.ContainsKey(item))
              {


              }

          }


tried something like this but genuinely have no idea how to start
Posted
Updated 20-Dec-19 10:28am

 
Share this answer
 
v3
C#
foreach (string item in statsB.Keys)
{
   if (statsC.ContainsKey(item))
   {
      if (statsB[item] == statsC[item])
      {
         // values match
      }
      else
      {
         // values don't match
      }
   }
}

or
C#
foreach (var item in statsB)
{
   string key = item.Key;
   if (statsC.ContainsKey(key))
   {
      if (statsB[key] == statsC[key])
      {
         // values match
      }
      else
      {
         // values don't match
      }
   }
}

You should always refer to the documentation when you don't know how to use an object (assuming said object is documented).
Dictionary<TKey,TValue> Class[^]
KeyValuePair<TKey,TValue> Struct[^] (the type of item in the second solution)
This is also why using var for everything doesn't help beginners to clearly understand the code they write.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900