C# Dictionary Tutorial with Examples

Today, We want to share with you C# Dictionary Tutorial with Examples.
In this post we will show you Dictionary in C#.Net with example and description, hear for C# Dictionary Beginners Tutorials With Code Examples we will give you demo and example for implement.
In this post, we will learn about Understanding Dictionaries in .NET with an example.

Introduction: Dictionary in C#.Net with an example and description

In this post, we will learn about Dictionary in C#.Net with an example.Dictionary is a type collection which is used to store data in key and value pairs.In dictionary object Key value must be always unique. It does not allow null or duplicate combination.
The Dictionary collection is a part of generic collections so to use Dictionary object in our applications we need to add the following namespace in our applications within code.
using System.Collections.Generic;

READ :  AngularJS Cookies Set Get and Remove Cookies

Useful methods of Dictionary:

Add(TKey,TVal) :

This method is used to add specified key and value to the dictionary

Remove(TKey) :

This method is used to removes the value from Dictionary<TKey, TValue> based on specified key

TryGetValue(TKey, TValue) :

This method is used to get the value associated with the specified key.

Add(TKey,TVal) :

This method is used to get the value associated with the specified key.

Clear :

This method is used to removes all keys and values from the Dictionary<TKey, TValue>.

ContainsKey(TKey) :

This method is used to determines whether the Dictionary<TKey, TValue> contains specified key or not.

Now, Create Console Application in Visual Studio and write below line of code.

READ :  jQuery Display Image preview before upload in Asp.net

using System;
using System.Collections.Generic;

namespace ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Dictionary Demo
            Dictionary _SiteList = new Dictionary();

            _SiteList.Add("DotNetK.com", 1);
            _SiteList.Add("Live24u.com", 2);
            _SiteList.Add("w3school.com", 3);

            // Read all Site List
            Console.WriteLine("Site List");

            foreach (KeyValuePair site in _SiteList)
            {
                Console.WriteLine("Key = {0}, Value = {1}",
                    site.Key, site.Value);
            }
            Console.ReadKey();
        }
    }   
}

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Introduction To C# Dictionary.
I would like to have feedback on my Infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.