C# HashSet - c# - c# tutorial - c# net




What in C# Hashset ?

  • C# HashSet class can be used to store, remove or view elements.
  • It does not store duplicate elements.
  • It is suggested to use HashSet class if you have to store only unique elements.
  • It is found in System.Collections.Generic namespace.
  • learn c# tutorials - hash function - c# programs

    learn c# tutorial -csharp hash function - c# programs - c# Example

  • This is an optimized set collection. It helps eliminates duplicate strings or elements in an array. It is a set that hashes its contents.

Syntax:

[SerializableAttribute]
[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
public class HashSet<T> : ICollection<T>, IEnumerable<T>, IEnumerable, 
	ISerializable, IDeserializationCallback, ISet<T>, IReadOnlyCollection<T>
click below button to copy the code. By - c# tutorial - team

C# HashSet<T> example

  • Let's see an example of generic HashSet<T> class that stores elements using Add() method and iterates elements using for-each loop.
using System;  
using System.Collections.Generic;  
  
public class HashSetExample  
{  
    public static void Main(string[] args)  
    {  
       // Create a set of strings  
        var names = new HashSet<string>();  
        names.Add("Sonoo");  
        names.Add("Ankit");  
       names.Add("Peter");  
        names.Add("Irfan");  
        names.Add("Ankit");//will not be added  
          
        // Iterate HashSet elements using foreach loop  
        foreach (var name in names)  
        {  
            Console.WriteLine(name);  
        }  
    }  
}  
click below button to copy the code. By - c# tutorial - team

C# examples - Output :

Sonoo
Ankit
Peter
Irfan

C# HashSet<T> example 2:

  • Let's see another example of generic HashSet<T> class that stores elements using Collection initializer.
using System;  
using System.Collections.Generic;  
  
public class HashSetExample  
{  
    public static void Main(string[] args)  
    {  
        // Create a set of strings  
        var names = new HashSet<string>(){"Sonoo", "Ankit", "Peter", "Irfan"};  
          
        // Iterate HashSet elements using foreach loop  
        foreach (var name in names)  
        {  
            Console.WriteLine(name);  
        }  
    }  
}  
click below button to copy the code. By - c# tutorial - team
Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

C# examples - Output :

Sonoo
Ankit
Peter
Irfan

learn c# tutorials - encryption hash function - c# programs

learn c# tutorial -csharp encryption hash function - c# programs - c# Example



Related Searches to C# HashSet

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×