Tuesday 12 March 2019

hashtable and dictionary

public class HashTableAndDictionary
    {
       IList<string> strIList = new List<string>();
        List<int> intList = new List<int>();
        public static void Test()
        {
           
            HashTableTest();
            Dictionarytest();
        }
        public static Hashtable GetHashtable()
        {
          
            // Create and return new Hashtable.
            Hashtable hashtable = new Hashtable();
            hashtable.Add("a", 1);
            hashtable.Add("b", 2);
            return hashtable;
        }
        public static void HashTableTest()
        {
            var hashObject = GetHashtable();
            try
            {
                var s1 = hashObject["a"];
                var s2 = hashObject["c"];
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public static void Dictionarytest()
        {
            Dictionary<string, int> d = new Dictionary<string, int>()
               {
                        {"a", 2},
                        { "b", 1}
                };
            // Loop over pairs with foreach
            foreach (KeyValuePair<string, int> pair in d)
            {
                Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
            }
            try
            {
                var s1 = d["a"];
                var s2 = d["c"];
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }

No comments:

Post a Comment