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

Is c# int immutable?

510


write a c# program add two matrix with input number ?

565


Please explain the basic string operations used in c#?

550


Explain About Postback

557


Give some examples of commonly used i/o classes?

496






How does a while loop work?

481


Describe the ways of cleaning up objects in c#.

520


What is the real use of interface in c#?

465


What is the C# syntax to catch any possible exception?

528


what class is underneath the sortedlist class?

551


What is dll in c#?

509


What is a framework in c#?

470


What is c# used for?

515


How do I move from one form to another in c#?

467


How can I create a process that is running a supplied native executable (e.g., Cmd.exe)?

599