Translate

Saturday 28 June 2014

Differences between Hashtable and Dictionary

Dictionary:

·         It returns error if we try to find a key which does not exist.
·         It is faster than a Hashtable because there is no boxing and unboxing.
·         Only public static members are thread safe.
·         Dictionary is a generic type which means we can use it with any data type.
·         static typing (and compile-time verification)

Hashtable:

·         It returns null if we try to find a key which does not exist.
·         It is slower than dictionary because it requires boxing and unboxing.
·         All the members in a Hashtable are thread safe,

·         Hashtable is not a generic type,

        DICTIONARY VS HASHTABLE in C# REAL TIME EXAMPLES

        Differences Between Hashtable and Dictionary

        1. Hashtable is threadsafe and while Dictionary is not.
        // Creates a synchronized wrapper around the Hashtable.
          Ex:Hashtable myhashtable = Hashtable.Synchronized(hash);
        The Synchronized method is thread safe for multiple readers and writers. Furthermore, the synchronized wrapper ensures that there is only one writer writing at a time.

        2. Dictionary is types means that the values need not to boxing while Hashtable 
            values  need to be boxed or unboxed because it stored the  values and keys as 
            objects.         
        3. When you try to get the value of key which does not exists in the collection, the 
            dictionary throws an exception of 'KeyNotFoundException' while hashtable returns 
            null value.
        4. When using large collection of key value pairs hashtable would be considered more 
            efficient than dictionary.

        5. When we retrieve the record in collection the hashtable does not maintain the order 
            of entries while dictionary maintains the order of entries by which entries were added.

        6. Dictionary relies on chaining whereas Hashtable relies on rehashing.

        7.Dictionary is a strongly typed generic collection while hashtable collection takes a object datatype

     Example

        public void MethodHashTable()
        {
            Hashtable objHashTable = new Hashtable();
            objHashTable.Add(1, 100);    // int
            objHashTable.Add(2.99, 200); // float
            objHashTable.Add('A', 300);  // char
            objHashTable.Add("4", 400);  // string

            lblDisplay1.Text = objHashTable[1].ToString();
            lblDisplay2.Text = objHashTable[2.99].ToString();
            lblDisplay3.Text = objHashTable['A'].ToString();
            lblDisplay4.Text = objHashTable["4"].ToString();

            // ----------- Not Possible for HashTable ----------
            //foreach (KeyValuePair<string, int> pair in objHashTable)
            //{
            //    lblDisplay.Text = pair.Value + " " + lblDisplay.Text;
            //}
        }


        Dictionary Example 

        public void MethodDictionary()
          {
            Dictionary<string, int> dictionary = new Dictionary<string, int>();
            dictionary.Add("cat", 2);
            dictionary.Add("dog", 1);
            dictionary.Add("llama", 0);
            dictionary.Add("iguana", -1);

            //dictionary.Add(1, -2); // Compilation Error

            foreach (KeyValuePair<string, int> pair in dictionary)
            {
                lblDisplay.Text = pair.Value + " " + lblDisplay.Text;
            }

          }






What is the difference between ArrayList and Generic List in C#?

Arraylist:
  1. It is like Array of objects.
  2. "System.Collections" is the Namespace for Arraylist. It automatically added to namespace section in aspx.cs page.
  3. We need not to specify object type arraylist going to contain.
  4. In arraylist each item is stored as object. And return value as object.
  5. It needs to box and unbox the elements.
  6. No type safety (Compile time)
List<T>:



  1. "System.Collections.Generic" is the Namespace for List. We need to add this.
  2. We need to specify object type which type of elements it contain. 
  3. Ex: List<string> StrObj=new List<string>();
  4. From the above example StrObj will only store string objects.
  5. It doesn't need.
  6. Type safety (compile time)
Example: 

namespace Example
{
   class Program

    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            list.Add("hello");
            list.Add(new Program()); // Oops! That's not meant to be there...
            list.Add(4);
            foreach (object o in list)
            {
                Console.WriteLine(o.ToString());
            }
            List<string > lstString = new List<string>();
            lstString.Add("ABCD");
            lstString.Add(new Program());   //Compiler error
            lstString.Add(4);   // Compiler Error
            foreach (object o in lstString)
            {
                Console.WriteLine(o.ToString());
            }  }

}


WIPO Patent Translation (language)

https://www3.wipo.int/patentscope/translate/translate.jsf?interfaceLanguage=en