Dictionary in C#

Introduction:

A Dictionary class represents a dictionary in C# that is used to represent a collection of keys and values pair of data. This article demonstrates how to use a dictionary in C#.





Example:

The following code snippet creates a Dictionary, adds some data to it and reads data back.

----------------------------------------------------------------------------------------------------------------------
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections.Generic;

 

namespace DictionarySample

{

    class Program

    {

        static void Main(string[] args)

        {

           

            Dictionary<string, Int16> AuthorList = new Dictionary<string, Int16>();

 

            AuthorList.Add("Mahesh Chand", 35);

            AuthorList.Add("Mike Gold", 25);

            AuthorList.Add("Praveen Kumar", 29);

            AuthorList.Add("Raj Beniwal", 21);

            AuthorList.Add("Dinesh Beniwal", 84);

 

            // Read all data

            Console.WriteLine("Authors List");

 

            foreach( KeyValuePair<string, Int16> author in AuthorList )

            {

                Console.WriteLine("Key = {0}, Value = {1}",

                    author.Key, author.Value);

            }

 

            Console.ReadKey();

 

        }

    }

}

----------------------------------------------------------------------------------------------------------------------
Ashwani
Ashwani

This is a short biography of the post author. Maecenas nec odio et ante tincidunt tempus donec vitae sapien ut libero venenatis faucibus nullam quis ante maecenas nec odio et ante tincidunt tempus donec.

No comments:

Post a Comment