Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous collection initializer for a dictionary

Is it possible to implicitly declare next Dictionary<HyperLink, Anonymous>:

{ urlA, new { Text = "TextA", Url = "UrlA" } },
{ urlB, new { Text = "TextB", Url = "UrlB" } }

so I could use it this way:

foreach (var k in dic)
{
   k.Key.Text = k.Value.Text;
   k.Key.NavigateUrl = k.Value.Url;
}

?

like image 889
abatishchev Avatar asked Nov 29 '09 11:11

abatishchev


People also ask

How do you initialize a dictionary?

Initialization. Dictionaries are also initialized using the curly braces {} , and the key-value pairs are declared using the key:value syntax. You can also initialize an empty dictionary by using the in-built dict function. Empty dictionaries can also be initialized by simply using empty curly braces.

How do I start a dictionary in C#?

With C# 6.0, you can create a dictionary in the following way: var dict = new Dictionary<string, int> { ["one"] = 1, ["two"] = 2, ["three"] = 3 }; It even works with custom types.

Why searching from dictionary collection can be faster than using list collection?

The reason is because a dictionary is a lookup, while a list is an iteration. Dictionary uses a hash lookup, while your list requires walking through the list until it finds the result from beginning to the result each time.

What is the correct way of initialising the list of integers using collection initializers?

By using a collection initializer, you do not have to specify multiple calls; the compiler adds the calls automatically. List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; List<int> digits2 = new List<int> { 0 + 1, 12 % 3, MakeInt() };


2 Answers

How about:

var dict = new[] {
            new { Text = "TextA", Url = "UrlA" },
            new { Text = "TextB", Url = "UrlB" }
        }.ToDictionary(x => x.Url);
// or to add separately:
dict.Add("UrlC", new { Text = "TextC", Url = "UrlC" });

However, you could just foreach on a list/array...

var arr = new[] {
    new { Text = "TextA", Url = "UrlA" },
    new { Text = "TextB", Url = "UrlB" }
};
foreach (var item in arr) {
    Console.WriteLine("{0}: {1}", item.Text, item.Url);
}

You only need a dictionary if you need O(1) lookup via the (unique) key.

like image 105
Marc Gravell Avatar answered Sep 27 '22 23:09

Marc Gravell


Yes, but only with great workaround, and only within a method.

This is how you can do it:

    static Dictionary<TKey, TValue> NewDictionary<TKey, TValue>(TKey key, TValue value)
    {
        return new Dictionary<TKey, TValue>();
    }
    public void DictRun()
    {

        var myDict = NewDictionary(new { url="a"},
            new { Text = "dollar", Url ="urlA"});
        myDict.Add(new { url = "b" }, new { Text = "pound", Url = "urlB" });
        myDict.Add(new { url = "c" }, new { Text = "rm", Url = "urlc" });
        foreach (var k in myDict)
        {
            var url= k.Key.url;
            var txt= k.Value.Text;

            Console.WriteLine(url);
            Console.WriteLine(txt);    
        }

    }

You can refer to this SO question for more info.

like image 35
Graviton Avatar answered Sep 27 '22 23:09

Graviton