4/10/2015 - 5:16 PM

Hashtable - Multiple Types

Hashtable - Multiple Types

	Multiple Types 
		○ Adding heterozygous values to a hash table -- like string keys and int keys 
		○ Though all K|V pairs have different types they can all be added to the same hashtable
		○ Again to reference and return the value a cast operation is used.
			§ You decide what datatype to used based on what datatype you want to return.  E.g.,  For indexer [300], the return value is a string, so it is cast to (string).  For indexer key "Area", the desired return value will be a numeric, so it is cast to (int).  

Possible error -- if improperly cast, will throw InvalidCastException. 

//LP : runas C# program
//using System;
//using System.Collections;

class Program
	static Hashtable GetHashtable() 
		Hashtable hashtable = new Hashtable();
		hashtable.Add(300, "Carrot");
		hashtable.Add("Area", 1000);
		return hashtable;
	static void Main() 
		Hashtable hashtable = GetHashtable();
		string value1 = (string)hashtable[300];
		int value2 = (int)hashtable["Area"];