Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between using square brackets and curly braces in dictionary initializers?

Tags:

c#

In dictionary initializers, there are two ways to create the dictionary with content:

new Dictionary<string, GradientSpace>
{
    ["userSpaceOnUse"] = GradientSpace.Absolute,
    ["objectBoundingBox"] = GradientSpace.Relative
})

and

new Dictionary<string, GradientSpace>
{
    {"userSpaceOnUse", GradientSpace.Absolute},
    {"objectBoundingBox", GradientSpace.Relative}
});

Is there any difference at all in these two syntaxes, or is it just a matter of preference?

like image 457
laptou Avatar asked Sep 02 '18 21:09

laptou


People also ask

What is the difference between curly brackets and square brackets?

The curly brackets are used to denote a block of code in a function. So, say we need a function to calculate the standard error we might do this. The square brackets are used to subset vectors and data frames.

Do dictionaries use curly brackets?

A dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets and have keys and values. To define a dictionary, use curly braces and separate each term/definition pair with a comma.

What is the difference between square brackets and round brackets in Python?

square bracket is used for indexing an array/list/dict, round brackets are used in function calls.

Which set of brackets are used for dictionaries?

A dictionary is an unordered and mutable Python container that stores mappings of unique keys to values. Dictionaries are written with curly brackets ({}), including key-value pairs separated by commas (,). A colon (:) separates each key from its value.


1 Answers

When the source is as follows and the target framework is .NET 4.7.1:

var x = new Dictionary<string, int>
{
    ["userSpaceOnUse"] = 1,
    ["objectBoundingBox"] = 3
};

var y = new Dictionary<string, int> {
    {"userSpaceOnUse", 1},
    {"objectBoundingBox", 3}
};

This results in the following Intermediate Language (using JetBrains dotPeek):

// [18 13 - 22 15]
IL_0001: newobj       instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string, int32>::.ctor()
IL_0006: dup          
IL_0007: ldstr        "userSpaceOnUse"
IL_000c: ldc.i4.1     
IL_000d: callvirt     instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string, int32>::set_Item(!0/*string*/, !1/*int32*/)
IL_0012: nop          
IL_0013: dup          
IL_0014: ldstr        "objectBoundingBox"
IL_0019: ldc.i4.3     
IL_001a: callvirt     instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string, int32>::set_Item(!0/*string*/, !1/*int32*/)
IL_001f: nop          
IL_0020: stloc.0      // x

// [25 13 - 28 15]
IL_0021: newobj       instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string, int32>::.ctor()
IL_0026: dup          
IL_0027: ldstr        "userSpaceOnUse"
IL_002c: ldc.i4.1     
IL_002d: callvirt     instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string, int32>::Add(!0/*string*/, !1/*int32*/)
IL_0032: nop          
IL_0033: dup          
IL_0034: ldstr        "objectBoundingBox"
IL_0039: ldc.i4.3     
IL_003a: callvirt     instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string, int32>::Add(!0/*string*/, !1/*int32*/)
IL_003f: nop          
IL_0040: stloc.1      // y

The first way results in the indexer/property being set, while the second way uses the Add() method, meaning they are translated differently.

The .NET Core source of the Dictionary class is also interesting to look at in this context: https://github.com/Microsoft/referencesource/blob/master/mscorlib/system/collections/generic/dictionary.cs

like image 58
Bouke Avatar answered Sep 16 '22 22:09

Bouke