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 8130401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:41:40+00:00 2026-06-06T08:41:40+00:00

Why is it possible to initialize a Dictionary<T1,T2> like this: var dict = new

  • 0

Why is it possible to initialize a Dictionary<T1,T2> like this:

var dict = new Dictionary<string,int>() { 
    { "key1", 1 },
    { "key2", 2 }
};

…but not to initialize, say, an array of KeyValuePair<T1,T2> objects in exactly the same way:

var kvps = new KeyValuePair<string,int>[] {
    { "key1", 1 },
    { "key2", 2 }
};
// compiler error: "Array initializers can only be used in a variable 
// or field initializer.  Try using a new expression instead."

I realize that I could make the second example work by just writing new KeyValuePair<string,int>() { "key1", 1 }, etc for each item. But I’m wondering if it’s possible to use the same type of concise syntax that is possible in the first example.

If it is not possible, then what makes the Dictionary type so special?

  • 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-06-06T08:41:44+00:00Added an answer on June 6, 2026 at 8:41 am

    The collection initializer syntax is translated into calls to Add with the appropriate number of parameters:

    var dict = new Dictionary<string,int>();
    dict.Add("key1", 1);
    dict.Add("key2", 2);
    

    This special initializer syntax will also work on other classes that have an Add method and implements IEnumerable. Let’s create a completely crazy class just to prove that there’s nothing special about Dictionary and that this syntax can work for any suitable class:

    // Don't do this in production code!
    class CrazyAdd : IEnumerable
    {
        public void Add(int x, int y, int z)
        {
            Console.WriteLine(x + y + z); // Well it *does* add...
        }
    
        public IEnumerator GetEnumerator() { throw new NotImplementedException(); }
    }
    

    Now you can write this:

    var crazyAdd = new CrazyAdd
    {
        {1, 2, 3},
        {4, 5, 6}
    };
    

    Outputs:

    6
    15
    

    See it working online: ideone

    As for the other types you asked about:

    • It doesn’t work on an array because it has no Add method.
    • List<T> has an Add method but it has only one parameter.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this powershell code: $drivers = New-Object 'System.Collections.Generic.Dictionary[String,String]' $drivers.Add(nitrous,vx) $drivers.Add(directx,vd) $drivers.Add(openGL,vo) Is it possible
Why is not possible to initialize a property to a function when you declare
Is it possible to initialize a variable from a return parameter (by ref)? Say
Is it possible to initialize all UI elements of certain type (like all TextViews
Since C99 (and C++20), it's possible to initialize structs using this syntax: struct info
is it possible to initialize a List with other List's in C#? Say I've
Possible Duplicate: In what order does python display dictionary keys? There was this very
Was wondering if it is possible to initialize a text field with printing (this
i'm new to php and am wondering - is it possible to initialize a
I have this method with a huge switch statement like this: public bool ExecuteCommand(string

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.