Click here to Skip to main content
12,348,760 members (33,633 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
Hi,

Can anyone explain when will use List and Hash table.Which scenario use this concepts?
Posted 21-Sep-12 5:40am
Edited 21-Sep-12 5:44am
Wes Aday104.2K
v2
Comments
Sergey Alexandrovich Kryukov 21-Sep-12 11:58am
   
What's the problem with your reading of the documentation?
--SA
Rate this: bad
 
good
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>();
 
values.Add("Value1");
values.Add("Value2");
values.Add("Value3");
values.Add("Value4");
values.Add("Value5");
</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: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
Rate this: bad
 
good
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<>:
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  
v3
Comments
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.
--SA
Mehdi Gholam 21-Sep-12 12:53pm
   
5'ed
Sergey Alexandrovich Kryukov 21-Sep-12 13:01pm
   
Thank you, Mehdi.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Follow the discussions in below links...
1. List vs hashtable[^]
2. ArrayList Vs Hash Table[^]
Comments
Sergey Alexandrovich Kryukov 21-Sep-12 12:35pm
   
Obsolete or irrelevant discussions. OP uses v.4.
--SA
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.160621.1 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2016
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