Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

Can anyone explain when will use List and Hash table.Which scenario use this concepts?
Posted
Updated 21-Sep-12 5:44am
v2
Comments
Sergey Alexandrovich Kryukov 21-Sep-12 11:58am    
What's the problem with your reading of the documentation?
--SA

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.
C#
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")
C#
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")
 
Share this answer
 
Comments
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.
--SA
Sergey Alexandrovich Kryukov 21-Sep-12 12:33pm    
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
 
Share this answer
 
v3
Comments
fjdiewornncalwe 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.
--SA
Mehdi Gholam 21-Sep-12 12:53pm    
5'ed
Sergey Alexandrovich Kryukov 21-Sep-12 13:01pm    
Thank you, Mehdi.
--SA
Follow the discussions in below links...
1. List<t> vs hashtable[^]
2. ArrayList Vs Hash Table[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Sep-12 12:35pm    
Obsolete or irrelevant discussions. OP uses v.4.
--SA
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900