Iterate dictionary object efficiently in c# with pre defined keys? I have a dictionary that I need to iterate efficiently. I already know about the keys beforehand which I need to extract from values dictionary object.
What's the best way to do that? I have around 20 entries in dictionary object as of now.

Below is how I am doing it but wanted to see if we can optimize it by just getting values of key directly since I already know about what key I need to extract from them instead of using foreach loop here which is not good in terms of performance?

  // predefined keys
  public const string ABC = "header-abc";
  public const string PQR = "header-pqr";
  public const string DEF = "header-def";
  public const string HIP = "header-hip";

  foreach (KeyValuePair&lt;string, StringValues&gt; v in values)
  {
      string key = v.Key;
      StringValues val = v.Value;
      if (val.Count &gt; 0)
      {
          if (!string.IsNullOrWhiteSpace(val[0]))
          {
              switch (key)
              {
                  case ABC:
                      HeadOne = val[0];
                      HeadOneId = HeadOne;
                      break;
                  case PQR:
                      HeadTwo = val[0];
                      HeadTwoId = HeadTwo;
                      break;
                  case DEF:
                      HeadThree = val[0];
                      HeadThreeId = HeadThree;
                      break;
                  case HIP:
                      HeadFour = val[0];
                      HeadFourId = HeadFour;
                      break;
                  //.. bunch of other case block here with similar stuff
              }
          }
      }
  }


As of now it looks redundant to iterate values dictionary object since I already know about all my keys that I need to extract from values dictionary object. What's the best way (in terms of performance) to do this considering keys are already known beforehand?

One option I can think of is instead of foreach loop here can we use keys property of iterating dictionary?
I believe you can do this if you put the header keys inside an array, and then you can loop over the array itself, and use TryGetValue for the dictionary. 

This is the simplest form for it : 

public readonly string[] HeaderKeys = {
    "header-abc", 
    "header-pqr", 
    "header-def", 
    "header-hip"
}

for(var x = 0; x &lt; HeaderKeys.Length; x++)
{
    var isKeyExist = values.TryGetValue(HeaderKeys[x], var KeyValue);

    // if there is no key defined, just go to the next iteration.
    if(!isKeyExist) { continue; }   

    switch (HeaderKeys[x])
    {
      case "header-abc":
          // inline assigning
          HeadOne = HeadOneId = KeyValue; 
          break;
      case "header-pqr":
          HeadTwo = HeadTwoId = KeyValue;
          break;
      case "header-def":
          HeadThree = HeadThreeId = KeyValue;
          break;
      case "header-hip":
          HeadFour = HeadFourId = KeyValue;
          break;
      //.. bunch of other case block here with similar stuff
    }
}


You need a readonly string[] this way, it can't be changed once initialized. System.Array is the lightest, and yet the simplest array, so using for loop with it would give you the fastest iteration over any other collection type. 

Next, you only want to pass each header key to the TryGetValue and get the value. The TryGetValue would return true if the value has been out, and false if there is none. 

Then, you have one line if condition : 

    // if there is no key defined, just go to the next iteration.
    if(!isKeyExist) { continue; }   


This would skip any iteration if the result of TryGetValue is false. 
And I believe that string.IsNullOrWhiteSpace(KeyValue) is an extra unneeded work. So, I got rid of it. because even if the KeyValue is null or empty, the variables would be also null. So, at this stage you're just assigning variables, the other validation would be on other part of the current system where it needs to be critical to have a value. 

Now, assigning the variables like this :  

HeadOne = KeyValue;
HeadOneId = HeadOne;


can be simplified to this : 

HeadOne = HeadOneId = KeyValue;


the difference is that the first one would let the compiler read the value twice. While the second would read it once, and assign it twice.