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
codeaft@codeaft:~/csharp$ dotnet run Cherry Apple Orange Mango Grapes Pineapple Size: 6 codeaft@codeaft:~/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
codeaft@codeaft:~/csharp$ dotnet run Cherry Orange Mango Grapes Pineapple Size: 0 codeaft@codeaft:~/csharp$
Comments and Reactions