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 the dictionary object. Let us now look in the fact of how this could be used complete elaboration.

To better understand this tutorial, you should know following C# programming topic:

Creation of Object

Let us initially create just two 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 listed 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 the Dictionary

Now we can 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 a value from the 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 the 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 learned the basic concept of Dictionary Initializer of C# 6.0. We believe this was useful information. Happy reading!

Related Articles:

Last modified: March 8, 2019

Comments

This is simple. thank you

Write a Reply or Comment

Your email address will not be published.