Click here to Skip to main content
12,898,569 members (59,056 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# C#4.0

Can anyone explain when will use List and Hash table.Which scenario use this concepts?
Posted 21-Sep-12 5:40am
Updated 21-Sep-12 5:44am
Wes Aday104.2K
Sergey Alexandrovich Kryukov 21-Sep-12 11:58am
What's the problem with your reading of the documentation?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you need to just have a simple list that will be accessed by a numerical index only or only by the actual value of the item in the list then a List is what you would use.
A Hastable allows for a Value to be found based on a keyvalue rather than an index.
List<string> values = new List<string>();

In this instance the values can be accessed directly by using the index:(ie. values[2] will return you "Value3")
Hashtable values = new Hashtable();
values.Add("Key1", "Value1");
values.Add("Key2", "Value2");
values.Add("Key3", "Value3");
values.Add("Key4", "Value4");
values.Add("Key5", "Value5");

In this instance the values would be accessed by keyname instead: (ie. values["Key3"] will return you "Value3")
Mehdi Gholam 21-Sep-12 11:57am
Just posted the same, 5'ed
Sergey Alexandrovich Kryukov 21-Sep-12 12:33pm
Sorry, this is not the same -- please see my comments to this and your answers.
Sergey Alexandrovich Kryukov 21-Sep-12 12:33pm
As you say, trivial, but anyway good to take a look for a novice; my 5.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Nowhere. As of .NET network versions starting with 2.0, where the generics were be introduced, these classes are rendered obsolete. There were not formally marked with [Obsolete] attribute only because for supporting of legacy code they are just fine, but for all new developments they should not be used.

Instead, you need to use System.Collections.Generic.List<>:[^].

Instead of the hash table, you can use one (or more) of three generic classes:[^],[^],[^].

The difference between them are mostly in implementation and different trade-off between speed and redundancy.

See also:[^].

It's not feasible to explain or list all scenarios of using these collections. They are very basic data structures well known in most elementary computer science which you should learn.

The use if the collection is actually defined by its operation set and information of the computational complexity of each. To be able to select an appropriate type for a collection, you practically need to know all of collection classes and collection interfaces. It's important for proper encapsulation, maintenance, etc., to access classes through .NET interfaces where possible, instead of passing the references to the collection classes for direct use.

Well, list is a linear general-purpose collection with random access; think if it as of an array with variable length. The other collections mentioned above are the collection for fast access by the key, which is used as an index, which does not have to be numeric, and does not have to fill a range with consecutive values. Still, this is a search, and its computational omplexity is of O(1). What you also need it to understand this complexity and the "Big O" notation:[^],[^],[^].

Marcus Kramer 21-Sep-12 12:16pm
+5. I was just dealing with the trivial concept of the question, and the scope of my answer certainly didn't take this into account. You are absolutely correct that the Hashtable shouldn't be used at all.
Sergey Alexandrovich Kryukov 21-Sep-12 12:25pm
Thank you, Marcus.
Mehdi Gholam 21-Sep-12 12:53pm
Sergey Alexandrovich Kryukov 21-Sep-12 13:01pm
Thank you, Mehdi.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Follow the discussions in below links...
1. List<t> vs hashtable[^]
2. ArrayList Vs Hash Table[^]
Sergey Alexandrovich Kryukov 21-Sep-12 12:35pm
Obsolete or irrelevant discussions. OP uses v.4.
Tadit Dash 21-Sep-12 12:44pm
Oh Ok... Thanks...

I actually tried to give the OP some links explaining the concepts... That's why posted those.

Anyway sorry if I have made a mistake...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100