Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions src/System.Collections/src/System/Collections/Generic/HashSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,16 @@ public class HashSet<T> : ICollection<T>, ISet<T>, IReadOnlyCollection<T>
#region Constructors

public HashSet()
: this(EqualityComparer<T>.Default)
: this(0, EqualityComparer<T>.Default)
{ }

public HashSet(IEqualityComparer<T> comparer)
{
if (comparer == null)
{
comparer = EqualityComparer<T>.Default;
}
: this(0, comparer)
{ }

_comparer = comparer;
_lastIndex = 0;
_count = 0;
_freeList = -1;
_version = 0;
}
public HashSet(int capacity)
: this(capacity, EqualityComparer<T>.Default)
{ }

public HashSet(IEnumerable<T> collection)
: this(collection, EqualityComparer<T>.Default)
Expand Down Expand Up @@ -131,6 +125,30 @@ public HashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer)
}
}

public HashSet(int capacity, IEqualityComparer<T> comparer)
{
if (capacity < 0)
{
throw new ArgumentOutOfRangeException("capacity");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using C# 6, we can say:

throw new ArgumentOutOfRangeException(nameof(capacity)); 

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasonwilliams200OK corefx doesn't use C#6 yet.

}

if (comparer == null)
{
comparer = EqualityComparer<T>.Default;
}

_comparer = comparer;
_lastIndex = 0;
_count = 0;
_freeList = -1;
_version = 0;

if (capacity > 0)
{
Initialize(capacity);
}
}

#endregion

#region ICollection<T> methods
Expand Down