TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Dictionary Initializer

C# Dictionary Initializer for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C-SHARP

C# Dictionary Initializer

C# Dictionary initializer is a feature which is used to initialize dictionary elements. Dictionary is a collection of elements. It stores elements in key and value pair.

Dictionary initializer uses curly braces ({}) to enclose the key and value pair.

Let's see an example, in which we are initializing value for each key.

C# Dictionary Initializer Example 1

using System;
using System.Collections.Generic;
namespace CSharpFeatures
{
    class DictionaryInitializer
    {
        public static void Main(string[] args)
        {
            Dictionary dictionary = new Dictionary()
            {
                [1] = "Irfan",
                [2] = "Ravi",
                [3] = "Peter"
            };
            foreach (KeyValuePair kv in dictionary)
            {
                Console.WriteLine("{ Key = " + kv.Key + " Value = " +kv.Value+" }");
            }
        }
    }
}

Output:

{ Key = 1 Value = Irfan }
{ Key = 2 Value = Ravi }
{ Key = 3 Value = Peter }

In this example, we are storing student data into the dictionary. We are using dictionary initializer to store student data. See, the following example.

C# Dictionary Initializer Example 2

using System;
using System.Collections.Generic;
namespace CSharpFeatures
{
    class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
    }
    class DictionaryInitializer
    {
        public static void Main(string[] args)
        {
            Dictionary dictionary = new Dictionary()
            {
                { 1, new Student(){ ID = 101, Name = "Rahul Kumar", Email = "rahul@example.com"} },
                { 2, new Student(){ ID = 102, Name = "Peter", Email = "peter@example.com"} },
                { 3, new Student(){ ID = 103, Name = "Irfan", Email = "irfan@abc.com"} }
            };
            foreach (KeyValuePair kv in dictionary)
            {
                Console.WriteLine("Key = "+kv.Key + " Value = {" + kv.Value.ID +", "+ kv.Value.Name +", "+kv.Value.Email+"}");
            }
        }
    }
}

Output:

Key = 1 Value = {101, Rahul Kumar, [email protected]}
Key = 2 Value = {102, Peter, [email protected]}
Key = 3 Value = {103, Irfan, [email protected]}





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf