Dictionary in C#

Dictionary is a collection of keys and values in C#.

The Dictionary in C# is a collection of Keys and Values, where key is like word and value is like definition.

Dictionary cannot include duplicate or null keys, where as values can be duplicated or set as null.

Keys must be unique otherwise it will throw a runtime exception.

TKey denotes the type of key and TValue is the value.

Demo for Dictionary as below :

namespace Dictionary
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("Id", 111);
            dic.Add("Name", "Rahulsinh");
            dic.Add("Mobile", 9876543210);

            foreach (var item in dic)
            {
                Console.WriteLine(item.Key + " : " + item.Value);
            }
            Console.Read();
        }
    }
}

Output :

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories