Introduction to Dictionary Initializer in C#

Tutorials PanelPosted by

Dictionary Initializer got introduced in Visual Studio 2015, .NET 4.6 and C#6.0. With its use, we were able to map indexer during the initialization of dictionary object. Let us now look in the fact that how this could be used complete elaboration.

Creation of Object

Let us initially create just 2 classes such as ClassA and ClassB as mentioned below:

internal class ClassA
{
    public string ClassAMethod()
    {
        return "Return from Class A";
    }
}
internal class ClassB
{
    public string ClassBMethod()
    {
        return "Return from class B";
    }
}

You need to add objects to Dictionary with the use of new C# 6.0 Dictionary Initializer, and after this you need to create the Dictionary Initializer as mentioned below:

private static Dictionary<string, Type> ObjectTypes()
{

    return new Dictionary<string, Type>
    {
        ["ClassA"] = typeof(ClassA),
        ["ClassB"] = typeof(ClassB)               
    };
}

Versions earlier of C#6.0, we generally use dictionary Initializer for initializing the dictionary objects having values and key as mentioned below:

private static Dictionary<string, Type> ObjectTypes()
{
    return new Dictionary<string, Type>
    {
        {"ClassA", typeof(ClassA)},
        {"ClassB", typeof(ClassB)}               
    };
}

Though C# 6.0 also has increased such feature through making keys as the indexers which brings much readability in code.

Read values from Dictionary

Now we are able to read values from this Dictionary in a similar way as it was being used.

 private static dynamic GetObject(string key)
{            
    return Activator.CreateInstance(ObjectTypes()[key]) as dynamic;
}

We are also passing “key” and to retrieve value from Dictionary object and hence creating object request at the run time. As we wish to use object properties and the methods at the run time, we will be casting this in dynamic.

Client Invocation

Let us now invoke dictionary as the following:

GetObject("ClassA").ClassMethod();

And this is the complete implementation:

using System;
using System.Collections.Generic;

namespace dictionaryInitializerApp
{
    class Program
    {       
        static void Main(string[] args)
        {
             Console.WriteLine(GetObject("ClassA").ClassMethod());
             Console.ReadKey();

        }

        private static dynamic GetObject(string key)
        {            
            return Activator.CreateInstance(ObjectTypes()[key]) as dynamic;
        }

        private static Dictionary<string, Type> ObjectTypes()
        {

            return new Dictionary<string, Type>
            {
                ["ClassA"] = typeof(ClassA),
                ["ClassB"] = typeof(ClassB)               
            };
        }
    }

    internal class ClassA
    {
        public string ClassMethod()
        {
            return " Return from class A";
        }
    }
    internal class ClassB
    {
        public string ClassMethod()
        {
            return "Return from Class B";
        }
    }    
}

Conclusion

Here, we have learnt the basic concept of Dictionary Initializer of C# 6.0. We believe this was useful information. Happy reading!

Related Articles:

One comment

Leave a Reply