How is HashTable different from ArrayList in .NET?

The differences between HashTable and ArrayList are tabulated below:

HashTable
ArrayList
It is a map. It is a list.
You can add element to ArrayList using add method as shown below:
ArrayList sample = new ArrayList();
sample.add(10);
Here implicit key is the index position in which the element is stored which is always numeric.
You can add element to HashTable in the form of key-value pair using add method as shown below:
HashTable sample = new HashTable();
sample.add("key1","stringvalue");
Key can be numeric or alpha.
Retrieving elements is faster since you retrieve an element using its key. Retrieval is comparatively slow since you iterate over the elements in the list to retrieve a specific element.
In an ArrayList, you can store elements of the same type only. If you store elements of different types in the same ArrayList then you will get run time error while retrieving it. You will retrieve and display elements of ArrayList as shown below:
foreach(<datatype> element in <ArrayList>) {
Console.WriteLine(element)
}
In the same HashTable, you can store elements of different data types and retrieve them without any errors using DictionaryEntry as shown below:
foreach(Dictionaryentry dirSample in <HashTable>.keys.values){
Console.Writeline(dirSample.keys
+ "," + dirSample.values);
}

| How do you implement Observer Design Pattern in .NET? | How do you pass data between different Tiers in .NET Architecture? | How is Classic ADO different from ADO.NET? | How is Dataadapter useful in ADO.NET? | How is Datareader different from Dataset in ADO.NET? | How is .NET Application Development different from Traditional Development? | How is HashTable different from ArrayList in .NET? | How is Inheritance achieved in C#? | How is new keyword different from override keyword during inheritance in .NET? | How is String class different from StringBuilder class in .NET? | Illustrate ADO.NET Architecture | Illustrate the importance of Server.Transfer and Response.Redirect in .NET? | Mention the different objects available in Dataset of ADO.NET | Mention the usage of Connection Object in ADO.NET | What are the commonly used methods of Dataadapter in ADO.NET? | What are the different Behavioral Design Patterns that can be used in .NET Architecture? | What are the different Creational Design Patterns that can be used in .NET Architecture? | What are the different Structural Design Patterns that can be used in .NET Architecture? | What are the methods provided by Command Objects in ADO.NET? | What is Internal Access Modifier in C#? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.