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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:36:42+00:00 2026-06-03T09:36:42+00:00

Through some random object creation today I came across this neat little shortcut for

  • 0

Through some random object creation today I came across this neat little shortcut for a Dictionary<K, V>. Is the following assignment a compiler shortcut or is it a feature of Dictionary<string, string>.

IDictionary<string, string> items = { { "item1key", "item1value" } };

Looking at the source for Dictionary<K, V> I don’t see anything offhand for how this works. Implementing all the interfaces for this class dot not allow me to perform a similar operation. Why is it that we can do it for a dictionary but not another type. For example, how does the compiler or language feature know that the first item is a key and the second item is the value. Or even more specific this same syntax can’t be used for a List<string>

List<string> items = { "item1" };

So the first is valid, why?

I’m not necessarily trying to duplicate this but rather curious as to why it is the way it is. What makes a dictionary special in this case?

Example that works

public class Button
{
    public string Title { get; set; }
    public ButtonType Type { get; set; }
    public IDictionary<string, string> Items { get; set; }
    public bool RequiresSelected { get; set; }
}

var buttons = new List<Button>
    {
        new Button { 
            Items = {
                        {"button1", "Button 1"},
                        {"button2", "Button 2"},
                        {"button3", "Button 3"},
                    }, 
            Title = "3 Buttons", 
            Type = ButtonType.DropDown 
        }
    };
  • 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-03T09:36:44+00:00Added an answer on June 3, 2026 at 9:36 am

    The syntax you’ve shown isn’t valid in C#. You’d need:

    IDictionary<string, string> items = new Dictionary<string, string>
        { { "item1key", "item1value" } };
    

    At that point it’s just a normal collection initializer, so the list equivalent would be:

    List<string> items = new List<string> { "item1" };
    

    EDIT: Let’s see if my edit can beat yours. My guess is that you’ve seen something like:

    var foo = new Foo {
        SomeDictionaryProperty = { 
             { "item1key", "item1value" }
        }
    };
    

    That’s an embedded collection initializer, and can be used for lists too. It’s not creating a new dictionary, it’s adding to an existing one. The code above is equivalent to:

    var tmp = new Foo();
    tmp.SomeDictionaryProperty.Add("item1key", "item1value");
    var foo = tmp;
    

    Another example of it working:

    var form = new Form {
        Controls = { new Label { Text = "Foo"}, new Label { Text = "Bar" } }
    };
    

    See section 7.6.10.2 of the C# 4 specification (Object Initializers) for more information. The important bit is this:

    member-initializer:
        identifier   =   initializer-value
    
    initializer-value:
        expression
        object-or-collection-initializer
    

    So you can initialize a property to either be a specific value (in which case the setter will be used) or via an object/collection initializer, in which case the getter for the property will be used, and then setters or the Add method will be used for the body of the object/collection initializer.

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

Sidebar

Related Questions

I was going through some random java code and came across this peice of
Going through some documentation on modifying CGImageRef data, I came across a strange example
I was going through some data structures and I noticed this as a time
I'm working through some sample code and then this appeared: public abstract class RandomPool<T>
Going through some of my older Delphi projects and upgrading them to D2009, as
Reading through some of the questions here, the general concensus seems to be that
I looked through some code and noticed that the convention was to turn pointer
I was going through some code that I downloaded off the internet ( Got
I'm working through some tutorials and examples of java.util.concurrent package. Usually example authors put
I'm iterating through some objects and trying to set some properties that, apparently, don't

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.