Implementation of C# HashSet class: Add() method
Program.cs
using System.Collections.Generic;

HashSet<string> hs = new HashSet<string>();
hs.Add("Cherry");
hs.Add("Apple");
hs.Add("Orange");
hs.Add("Mango");
hs.Add("Grapes");
hs.Add("Cherry");
hs.Add("Apple");
hs.Add("Pineapple");
hs.Add("Cherry");

foreach (string s in hs)
{
    Console.Write(s + " ");
}
Console.WriteLine("\nSize: " + hs.Count);
Output
kodingwindow@kw:~/csharp$ dotnet run
Cherry Apple Orange Mango Grapes Pineapple 
Size: 6
kodingwindow@kw:~/csharp$ 
Implementation of C# HashSet class: Remove() and Clear() methods
Program.cs
using System.Collections.Generic;

HashSet<string> hs = new HashSet<string>();
hs.Add("Cherry");
hs.Add("Apple");
hs.Add("Orange");
hs.Add("Mango");
hs.Add("Grapes");
hs.Add("Cherry");
hs.Add("Apple");
hs.Add("Pineapple");
hs.Add("Cherry");

hs.Remove("Apple");
foreach (string s in hs)
{
    Console.Write(s + " ");
}
hs.Clear();
Console.WriteLine("\nSize: " + hs.Count);
Output
kodingwindow@kw:~/csharp$ dotnet run
Cherry Orange Mango Grapes Pineapple 
Size: 0
kodingwindow@kw:~/csharp$ 
Advertisement