---
title: "How to define HashSet<T> collection in C#, which collection is used to store unique values?"  
description: "How to define HashSet<T> collection in C#, which collection is used to store unique values?"  
author: "Ethan Karla"  
published: 2021-08-27  
canonical: https://answers.mindstick.com/qa/93939/how-to-define-hashset-t-collection-in-c-sharp-which-collection-is-used-to-store-unique-values  
category: "programming"  
tags: ["c#", "java"]  
reading_time: 4 minutes  

---

# How to define HashSet<T> collection in C#, which collection is used to store unique values?

How to [define](https://answers.mindstick.com/qa/72286/for-define-what-rats-and-rabbits-use-their-claws) [HashSet](https://www.mindstick.com/interview/33711/what-is-similarities-between-arraylist-and-hashset)<T> [collection](https://www.mindstick.com/articles/1718/collections-in-java) in C#, which collection is used to [store](https://www.mindstick.com/interview/1205/what-is-the-command-to-take-backup-and-restore-for-sharepoint-site) [unique](https://yourviews.mindstick.com/view/81654/court-order-but-unique-conditions-crpc-section-437-case-study) [values](https://www.mindstick.com/forum/159360/how-to-write-sql-query-to-join-comma-separated-values)?

## Answers

### Answer by Ravi Vishwakarma

**HashSet<T>** HashSet is a generic class of **System.Collectoins.Generic** namespace. Which is stores data uniquely, in other words, doesn’t contain the duplicate value. In this example, we use the Random class to forgive the random value and store it in the HashSet object. If a random class returns the duplicate value then doesn’t store it in the HashSet object.

> **HashSet collection is used for storing the unique value, because HashSet doesn’t contain the duplicate value.**

**Syntax**

```
HashSet<T> hset=new HashSet<T>();
```

**Example**

```
using System;
using System.Collections.Generic;
public class Program
{
 public static void Main()
 {
  HashSet<int> hset=new HashSet<int>();
  Random rand=new Random();
  for( int i=0; i<10; i++)
  {
   hset.Add(rand.Next(50,100));
  }
  Console.WriteLine('Duplicate data will not show in the list :');
  foreach(var item in hset)
   Console.WriteLine(item);
 }
}
```

**Output**

```
Duplicate data will not show in the list :
87
85
54
73
56
75
50
```

\

### Answer by Steilla Mitchel

#### C# | HashSet Class

A **HashSet<T>** is an unordered collection of the **unique elements**. It comes under ***System.Collections.Generic*** namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list.

## Characteristics of HashSet Class:

- The HashSet<**T**> class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.
- The capacity of a HashSet<**T**> object is the number of elements that the object can hold.
- A HashSet<**T**> object’s capacity automatically increases as elements are added to the object.
- A HashSet<**T**> collection is not sorted and cannot contain duplicate elements.
- HashSet<**T**> provides many mathematical set operations, such as set addition (unions) and set subtraction.

#### Constructors

| Constructor | Description |
| --- | --- |
| HashSet() | Initializes a new instance of the HashSet class that is empty and uses the default equality comparer for the set type. |
| HashSet(IEnumerable) | Initializes a new instance of the HashSet class that uses the default equality comparer for the set type, contains elements copied from the specified collection, and has sufficient capacity to accommodate the number of elements copied. |
| HashSet(IEnumerable, IEqualityComparer) | Initializes a new instance of the HashSet class that uses the specified equality comparer for the set type, contains elements copied from the specified collection, and has sufficient capacity to accommodate the number of elements copied. |
| HashSet(IEqualityComparer) | Initializes a new instance of the HashSet class that is empty and uses the specified equality comparer for the set type. |
| HashSet(Int32) | Initializes a new instance of the HashSet class that is empty, but has reserved space for capacity items and uses the default equality comparer for the set type. |
| HashSet(Int32, IEqualityComparer) | Initializes a new instance of the HashSet class that uses the specified equality comparer for the set type, and has sufficient capacity to accommodate capacity elements. |
| HashSet(SerializationInfo, StreamingContext) | Initializes a new instance of the HashSet class with serialized data. |

## Example:

```cs
// C# code to create a HashSet
using System;
using System.Collections.Generic;

class GFG {

	// Driver code
	public static void Main()
	{

		// Creating a HashSet of odd numbers
		HashSet<int> odd = new HashSet<int>();

		// Inserting elements in HashSet
		for (int i = 0; i < 5; i++) {
			odd.Add(2 * i + 1);
		}

		// Displaying the elements in the HashSet
		foreach(int i in odd)
		{
			Console.WriteLine(i);
		}
	}
}
```

## Output:

1 3 5 7 9


---

Original Source: https://answers.mindstick.com/qa/93939/how-to-define-hashset-t-collection-in-c-sharp-which-collection-is-used-to-store-unique-values

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
