How to get values from hashtable?

 Posted by Rajesh_Kumar on 1/16/2014 | Category: C# Interview questions | Views: 711 | Points: 40
Answer:

We can access value with the help of Hashtable'Key.
We can differentiate Hashtable with keys and Values.

Suppose,i have one Hashtable named ht_courses as below:-

Hashtable ht_courses = new Hashtable();

ht_courses.add("course1","Dot Net");
ht_courses.add("course2","C#");
ht_courses.add("course3","VB.NET");
ht_courses.add("course4","ASP.NET");


Here course1,course2,course3 and course4 are the Keys and Dot Net,C#,VB.NET and ASP.NET are the Values.

//Now to access Value from Hashtable,we will write below code:

 if (ht_courses.Count > 0)

{
string course1_value = (string)ht_courses["course1"];
string course2_value = (string)ht_courses["course2"];
string course3_value = (string)ht_courses["course3"];
string course4_value = (string)ht_courses["course4"];
}


//Output will be for:-
//course1_value = Dot Net
//course2_value = C#
//course3_value = VB.NET
//course4_value = ASP.NET


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response