what is the difference between arraylist and hash table
using a simple program?

Answer Posted / sivaprasad

1)In Array We Can Add any datatype value,Every item in
arraylist is treated as object.
2)Hashtable is collection of key,value pairs
i)Key Can be any datatype
ii)Key Cannot be null refrrence
iii)but value can be null referrence

Retrieving by key in Hashtable is faster than retrieving in
Arraylist,
Example of Arraylist:
Arraylist ar = new Arraylist();
ar.add("X");
ar.add(1);
ar.add(2);
Now if we Retrieve from Arraylist like
foreach(string str in ar)
{

}
It will Compile but it will give Runtime
Error,because '1' and '2' in Arraylist are integers.

Example Hashtable:
Hashtable ht = new Hashtable();
ht.add(1,"sample");
ht.add("2","Test");
ht.add(3,4);
foreach(Dictionaryentry dr in ht.keys.values)
{
Console.Writeline(dr.keys + "=" + dr.values);
}

The Above Code wont give any runtime Error Because We
are iterating through DictionaryEntries(Hashtable
implements IDictionary) it is Collection of Key/Value Pairs

Is This Answer Correct ?    97 Yes 22 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are cookies in c#?

746


Why are dynamic link library used over static one?

710


Why do we need properties in c#?

645


What is a Assembly?

716


What are the classes contained in a single .NET DLL ?

685


Why reflection is used in c#?

675


Can I use ReaderWriterLock instead of Monitor.Enter/Exit for Threading?

716


What are the 3 different types of arrays?

766


What is the difference between int.parse and int.tryparse methods?

740


What is visual c# net?

663


How we can create an array with non-default values?

706


What is method and function in c#?

702


What are the 2 broad classifications of fields in c#?

712


Is hashset ordered c#?

660


What is public void in c#?

699