2

i have a small sample .

//Class
public class GetEntity
 {
   public string name1 { get; set; }
   public string name2 { get; set; }
   public GetEntity() { }
 }

and:

public void GetHash()
   {
     HashSet objHash = new HashSet();
     GetEntity obj = new GetEntity();
     obj.name1 = "Ram";
     obj.name2 = "Shyam";
     objHash.Add(obj);
     foreach (GetEntity objEntity in objHash)
      {
        Label2.Text = objEntity.name1.ToString() + objEntity.name2.ToString();
      }

   }

Code works fine.Same task is done through Dictionary and List.But i want to know when we use HashSet<> , Dictionary<> or List<>.Is there only performance issue or any other things which i dont understand.Thanks.

4b0
  • 21,981
  • 30
  • 95
  • 142

4 Answers4

6

i want to know when we use HashSet<> , Dictionary<> or <List>

They all have different purpose and used in different scenarios

HashSet

Is used when you want to have a collection with unique elements. HashSet stores list of unique elements and won't allow duplicates in it.

Dictionary

Is used when you want to have a value against a unique key. Each element in Dictionary has two parts a (unique) key and a value. You can store a unique key in it (just like Hashset) in addition you can store a value against that unique key.

List

Is just a simple collection of elements. You can have duplicates in it.

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
2

Set does not contain duplicated values.

Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
1

I am not a C# guy myself but following should be the difference.

PLease correct me if I am wrong

HashSet will only take unique values, values can be randomly accessed by index, works in constant time

Dictionary will take key value pairs, Values can be accessed randomly by key names, key names can not be duplicate. This also is a very fast DS. Works in constant time

List will take n values even if they are not unique, values has to be accessed sequentially. Time complexity for insert and retrieval would be o(n) in worst case scenario

Sap
  • 5,197
  • 8
  • 59
  • 101
0

They are all called collections, which are usually in the namespace System.Collections.Generic.

When to use a certain data structure essentially requires understanding what operations they support. For HashSet, it's basically a Set in mathematics, and supports efficient Add, Remove, and quick judgement on whether an element Exists in the set. Given it's a set, the elements must be unique in Hashsets.

For Dictionary, it's basically a mapping structure, i.e. a set of Key-Value Pairs. Dictionary provides efficient Query on key-value pairs with a given key and also Add/Remove of the key-value pairs.

Lists are an ordered collection of elements. Unlike Hashsets, judging the existence of an element in a list is inefficient. And unlike Dictionaries, the internal data structure is not key-value pairs but simple objects. Another difference is you can use index (like list[3]) to efficiently access elements in Lists. (although it's not true for LinkedList)

grapeot
  • 1,594
  • 10
  • 21