Click here to Skip to main content
Sign Up to vote bad
good
See more: C#4.0
Hi,
 
Can anyone explain when will use List and Hash table.Which scenario use this concepts?
Posted 21 Sep '12 - 5:40
Edited 21 Sep '12 - 5:44
Wes Aday59.2K

Comments
Sergey Alexandrovich Kryukov - 21 Sep '12 - 11:58
What's the problem with your reading of the documentation? --SA

3 solutions

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>();
 
values.Add("Value1");
values.Add("Value2");
values.Add("Value3");
values.Add("Value4");
values.Add("Value5");
</string></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")
  Permalink  
Comments
Mehdi Gholam - 21 Sep '12 - 11:57
Just posted the same, 5'ed
Sergey Alexandrovich Kryukov - 21 Sep '12 - 12:33
Sorry, this is not the same -- please see my comments to this and your answers. --SA
Sergey Alexandrovich Kryukov - 21 Sep '12 - 12:33
As you say, trivial, but anyway good to take a look for a novice; my 5. --SA
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<>:
http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.100%29.aspx[^].
 
Instead of the hash table, you can use one (or more) of three generic classes:
http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.100%29.aspx[^],
http://msdn.microsoft.com/en-us/library/f7fta44c%28v=vs.100%29.aspx[^],
http://msdn.microsoft.com/en-us/library/ms132319%28v=vs.100%29.aspx[^].
 
The difference between them are mostly in implementation and different trade-off between speed and redundancy.
 
See also:
http://en.wikipedia.org/wiki/Redundancy_%28information_theory%29[^].
 
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:
http://en.wikipedia.org/wiki/Computational_complexity_theory[^],
http://en.wikipedia.org/wiki/Big_O_notation[^],
http://en.wikipedia.org/wiki/Big_O_in_probability_notation[^].
 
—SA
  Permalink  
Comments
Marcus Kramer - 21 Sep '12 - 12:16
+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:25
Thank you, Marcus. --SA
Mehdi Gholam - 21 Sep '12 - 12:53
5'ed
Sergey Alexandrovich Kryukov - 21 Sep '12 - 13:01
Thank you, Mehdi. --SA
Follow the discussions in below links...
1. List vs hashtable[^]
2. ArrayList Vs Hash Table[^]
  Permalink  
Comments
Sergey Alexandrovich Kryukov - 21 Sep '12 - 12:35
Obsolete or irrelevant discussions. OP uses v.4. --SA
Tadit Dash - 21 Sep '12 - 12:44
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
Your Filters
Interested
Ignored
     
0 OriginalGriff 208
1 Maciej Los 156
2 Richard MacCutchan 145
3 Tadit Dash 140
4 Santhosh G_ 135
0 Sergey Alexandrovich Kryukov 10,264
1 OriginalGriff 7,957
2 CPallini 4,201
3 Rohan Leuva 3,522
4 Maciej Los 3,155


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid