Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6020619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:35:41+00:00 2026-05-23T03:35:41+00:00

Which of these patterns should be preferred over the other and why? foreach(KeyValuePair<string, Dictionary<string,

  • 0

Which of these patterns should be preferred over the other and why?

foreach(KeyValuePair<string, Dictionary<string, string>> data in
    new Dictionary<string, Dictionary<string, string>> { 
        {"HtmlAttributes", this.HtmlAttributes},
        {"Content", this.Content}
    }) 
{
     foreach(KeyValuePair<string, string> entry in data.Value)
     {
         // do something
     }
}

or

Dictionary<string, Dictionary<string, string>> data = new Dictionary<string, Dictionary<string, string>>();
data.Add("HtmlAttributes", this.HtmlAttributes);
data.Add("Content", this.Content);

foreach(KeyValuePair<string, IDictionary<string, string>> entry in data) 
{
    // Do something
}           

data.Clear(); // not sure if this is needed either
data = null;  // gc object 

Please don’t answer with “use var”, as I don’t like using it.

Re: var (2 years later): I must add something to make this right. In retrospect, reading Eric Lipert’s blog post about when and why to use var makes total sense. IF used appropriately, meaning not all the time, it makes perfect sense and it shortens the amount code one needs to read. On the matter of what initialization to use, well the object initializer is fine, but splitting up initialization from the foreach or other processing makes the code more readable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:35:42+00:00Added an answer on May 23, 2026 at 3:35 am

    I would prefer something in between your two versions: split creation and iteration, but use collection initializer.

    Dictionary<string, Dictionary<string, string>> dicts =
        new Dictionary<string, Dictionary<string, string>> { 
        {"HtmlAttributes", this.HtmlAttributes},
        {"Content", this.Content}
    });
    
    foreach(KeyValuePair<string, Dictionary<string, string>> data in dicts)
    {
         foreach(KeyValuePair<string, string> entry in data.Value)
         {
             // do something
         }
    }
    

    or equivalently (really, from the point of view of both the compiler and IDE, the following is exactly the same):

    var dicts = new Dictionary<string, Dictionary<string, string>> { 
        {"HtmlAttributes", this.HtmlAttributes},
        {"Content", this.Content}
    });
    
    foreach(var data in dicts)
    {
         foreach(var entry in data.Value)
         {
             // do something
         }
    }
    

    Also, if you’re using Dictionary just as a list of pairs, you could use either List<KeyValuePair<K, V>> or (on .Net 4) List<Tuple<T1, T2>>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find an implementation of java.io.FileFilter which understands patterns like these: **/*.xml
Consider the following regex: (([^\|])*\|)*([^\|]*) This matches repetitive string patterns of the type (whatever
I have list of files which contain particular patterns, but those files have been
I have a view that contains two NSTextFieldCell s. The size at which these
Which of these pieces of code is faster? if (obj is ClassA) {} if
Which of these two platforms/ecosystems are better for writing web applications/websites? I am not
Which amongst these would be well suited for a scalable server side TCP socket
Which of these queries is more efficient, and would a modern DBMS (like SQL
Which of these two statements is faster/better practice? myList.Where(x => { bool itemOne= x.ItemOne
I was wondering which of these two scenario's works best for swapping between 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.