Skip to main content

Dictionary In C#

Introduction:

 Dictionary is a generic type which means we can use it with any data type .Dictionary take two parameter,  Key and Value e.g: Dictionary<Tkey,TValue>. Dictionary is defined in the System.Collections.Generic namespace.

using System.Collections.Generic; 


Syntax:

Dictionary<string, string> <NameOfDictionary> = new Dictionary<string, string>();

Code:

 private static void Main(string[] args)
        {
            Dictionary<string, int> dictionary = new Dictionary<string, int>();
            dictionary.Add("A", 1);   //Add Data in Dictionary
            dictionary.Add("B", 2);
            dictionary.Add("C", 3);
            dictionary.Add("D", 4);
            dictionary.Add("E", 5);
           
            foreach (var VARIABLE in dictionary)
            {

                Console.Write(VARIABLE.Key.ToString());
                Console.Write(VARIABLE.Value.ToString());
                Console.ReadLine();
            }

        }

Output: 


Comments

Popular posts from this blog

How to Pass Data from one Form to other Form in windows Form Application C#

Introduction: Hi Guys! In this tutorial we learn how to pass data from one form to other form in windows form application using c#. Let’s Follow the Step and create windows form application. Step1: Visual studio- à File- à New- à Project à windows Form Application- à Ok Step2: Click on ToolBox- à Drag and drop Label and TextBox. I creat a Form with 3 Labels and TextBox as shown in fig. Step 3: I have Name,Fname,Address Label on Form .so I take a three global variable .Write this code on the Form1.cs         public static string SetValueForText1 = "" ;         public static string SetValueForText2 = "" ;         public static string SetValueForText3 = "" ; Step4: Project- à Add Windows Form- à Click on Add Step 5: After Creating Form Double Click on Submit button on windows Form1 and Write the Cod...