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.
i want to know when we use HashSet<> , Dictionary<> or <List>They all have different purpose and used in different scenarios
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.
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.
Is just a simple collection of elements. You can have duplicates in it.