Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

C# Tutorial

C# HOME C# Intro C# Get Started C# Syntax C# Output C# Comments C# Variables C# Data Types C# Type Casting C# User Input C# Operators C# Math C# Strings C# Booleans C# If...Else C# Switch C# While Loop C# For Loop C# Break/Continue C# Arrays

C# Methods

C# Methods C# Method Parameters C# Method Overloading C# Scope C# Recursion

C# Classes

C# OOP C# Classes/Objects C# Class Members C# Constructors C# this Keyword C# Access Modifiers C# Properties C# Static C# Inheritance C# Polymorphism C# Abstraction C# Interface C# Enums C# Structs

C# Errors

C# Errors C# Debugging C# Exceptions

C# Data Structures

C# Data Structures C# List C# Dictionary C# HashSet C# Stack C# Queue

C# Advanced

C# Files C# Date and Time C# Nullable Types C# Generics C# Lambda C# LINQ

C# Cert

C# Certificate

C# How To

C# How Tos

C# Examples

C# Examples C# Compiler C# Exercises C# Quiz C# Practice Problems C# Server C# Syllabus C# Study Plan


C# HashSet


Share

HashSet

A List can have the same value many times. But sometimes you want each value only once.

Think of a list of all the countries you have visited. If you visit Japan two times, Japan should still be on the list only once.

A HashSet is a collection where each value appears only once. If you add a value that is already there, nothing happens.

Like List, HashSet is in the System.Collections.Generic namespace:

Example

// Include the namespace that holds HashSet
using System.Collections.Generic;

Create a HashSet

Create a HashSet and add values with Add(), just like with a list:

Example

HashSet<string> countries = new HashSet<string>();
countries.Add("Japan");
countries.Add("Norway");
countries.Add("Brazil");
countries.Add("Japan");

Console.WriteLine(countries.Count);  // 3

Try it Yourself »

Japan is added two times, but the HashSet keeps it only once. So Count is 3, not 4.

Add() also tells you if the value was added. It returns true for a new value, and false if the value is already in the set:

Example

HashSet<string> countries = new HashSet<string>();
Console.WriteLine(countries.Add("Japan"));   // True
Console.WriteLine(countries.Add("Norway"));  // True
Console.WriteLine(countries.Add("Japan"));   // False

Try it Yourself »



Check and Remove Values

Like with a list, you can also add the values when you create the set, inside curly braces { }.

Contains() returns true if the value is in the set, and false if it is not:

Example

HashSet<string> countries = new HashSet<string> { "Japan", "Norway", "Brazil" };
Console.WriteLine(countries.Contains("Norway"));  // True
Console.WriteLine(countries.Contains("Spain"));   // False

Try it Yourself »

Remove() removes a value from the set:

Example

HashSet<string> countries = new HashSet<string> { "Japan", "Norway", "Brazil" };
countries.Remove("Norway");

Console.WriteLine(countries.Count);               // 2
Console.WriteLine(countries.Contains("Norway"));  // False

Try it Yourself »


Loop Through a HashSet

A HashSet has no index, so you cannot write countries[0]. Use a foreach loop to go through the values:

Example

HashSet<string> countries = new HashSet<string> { "Japan", "Norway", "Brazil" };

foreach (string country in countries)
{
  Console.WriteLine(country);
}

Try it Yourself »



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.