58.4k views
2 votes
Create a dictionary with types as integer and string.

1 Answer

1 vote

Answer:

In c# Dictionary is the collection of keys and value .The dictionary is a generic collection class which is in the System.Collection.Generics namespace. we can represent dictionary like that Dictionary<TKey, TValue> where TKey represent the type of key and TValue is the type of TValue.

Following are the example which represent dictionary as integer and string

using System.Collections.Generic; // namespace

class Test // class test

{

static void Main() // main method

{

var ob = new Dictionary< int,string>(); // type integer and string

dictionary.Add( 2,"hello");

dictionary.Add(143,"hello1);

// The dictionary has 2 pairs.

Console.WriteLine("DICTIONARY 1 " + ob.Count);

}

}

Output:

DICTIONARY 1 :2

Step-by-step explanation:

In this program we create a dictionary generic class which is integer and string type after that we add the elements in the dictionary by "dictionary.Add method " and finally print the count of dictionary

User Adam Boduch
by
6.1k points