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
What is namespace give the example?
What is event delegate in c#?
Are c# strings null terminated?
What is variable in c#?
What are the Features in .net framework 1.1
What is windows form in c#?
Which is faster dictionary or list?
What is a variable in c#?
What is c# commonly used for?
How does dictionary work in c#?
Helo , Help Me , Help Me About : C# Windows Application - Network How To Manage IP Client's Accessiblity To The Internet Share concise Substitute , Minor ISA Server
Why do we use virtual methods in c#?
Define sealed classes in c#?
What is difference between dictionary and hashtable in c#?
What are value types in c#?