# NativeHashSet<T>

> An unordered, expandable set of unique values.

## Definition

* **Type:** Struct
* **Namespace:** [Unity.Collections](/engine/6000.7/script-reference/unity/collections.md)
* **Assembly:** UnityEngine.ManagedKernelModule
* **Implements:** [INativeDisposable](/engine/6000.7/script-reference/unity/collections/inativedisposable.md), [IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable), [IEnumerable\<T>](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1), [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.ienumerable)

```csharp
public struct NativeHashSet<T> : INativeDisposable, IDisposable, IEnumerable<T>, IEnumerable where T : unmanaged, IEquatable<T>
```

## Remarks

Not suitable for parallel write access. Use [NativeParallelHashSet\<T>](/engine/6000.7/script-reference/unity/collections/nativeparallelhashset1.md) instead.

## Static Fields

| Value                                                                                          | Description                                                     |
| ---------------------------------------------------------------------------------------------- | --------------------------------------------------------------- |
| [MaxCapacity](/engine/6000.7/script-reference/unity/collections/nativehashset1/maxcapacity.md) | The maximum number of elements this type of container can hold. |

## Constructors

| Constructor                                                                                                                                                   | Description                                                   |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- |
| [NativeHashSet\`1(System.Int32,Unity.Collections.AllocatorManager.AllocatorHandle)](/engine/6000.7/script-reference/unity/collections/nativehashset1/ctor.md) | Initializes and returns an instance of NativeParallelHashSet. |

## Properties

| Property                                                                                   | Description                                                    |
| ------------------------------------------------------------------------------------------ | -------------------------------------------------------------- |
| [Capacity](/engine/6000.7/script-reference/unity/collections/nativehashset1/capacity.md)   | The number of values that fit in the current allocation.       |
| [Count](/engine/6000.7/script-reference/unity/collections/nativehashset1/count.md)         | Returns the current number of values in this set.              |
| [IsCreated](/engine/6000.7/script-reference/unity/collections/nativehashset1/iscreated.md) | Whether this set has been allocated (and not yet deallocated). |
| [IsEmpty](/engine/6000.7/script-reference/unity/collections/nativehashset1/isempty.md)     | Whether this set is empty.                                     |

## Methods

| Method                                                                                             | Description                                                                                                                                                                       |
| -------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Add](/engine/6000.7/script-reference/unity/collections/nativehashset1/add.md)                     | Adds a new value. If the value is already present in the collection, this method **should** throw, but currently does not! Instead, it returns false without replacing said item. |
| [AsReadOnly](/engine/6000.7/script-reference/unity/collections/nativehashset1/asreadonly.md)       | Returns a readonly version of this NativeHashSet instance.                                                                                                                        |
| [Clear](/engine/6000.7/script-reference/unity/collections/nativehashset1/clear.md)                 | Removes all values.                                                                                                                                                               |
| [Contains](/engine/6000.7/script-reference/unity/collections/nativehashset1/contains.md)           | Returns true if a particular value is present.                                                                                                                                    |
| [Dispose](/engine/6000.7/script-reference/unity/collections/nativehashset1/dispose.md)             | Releases all resources (memory and safety handles).                                                                                                                               |
| [GetEnumerator](/engine/6000.7/script-reference/unity/collections/nativehashset1/getenumerator.md) | Returns an enumerator over the values of this set.                                                                                                                                |
| [Remove](/engine/6000.7/script-reference/unity/collections/nativehashset1/remove.md)               | Removes a particular value.                                                                                                                                                       |
| [ToNativeArray](/engine/6000.7/script-reference/unity/collections/nativehashset1/tonativearray.md) | Returns an array with a copy of this set's values (in no particular order).                                                                                                       |
| [TrimExcess](/engine/6000.7/script-reference/unity/collections/nativehashset1/trimexcess.md)       | Sets the capacity to match what it would be if it had been originally initialized with all its entries.                                                                           |
| [TryAdd](/engine/6000.7/script-reference/unity/collections/nativehashset1/tryadd.md)               | Attempts to add a new item. Only succeeds if the item was not present.                                                                                                            |

## Structs

| Struct                                                                                                         | Description                                                                                  |
| -------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
| [NativeHashSet\<T>.Enumerator](/engine/6000.7/script-reference/unity/collections/nativehashset1/enumerator.md) | An enumerator over the values of a set.                                                      |
| [NativeHashSet\<T>.ReadOnly](/engine/6000.7/script-reference/unity/collections/nativehashset1/readonly.md)     | A read-only alias for the value of a NativeHashSet. Does not have its own allocated storage. |
