Tedd.HashSetExtensions 1.0.2

list.ToHashSet(), ContainsRange(list), AddRange(list), RemoveRange(list) with and without selector.
Fast and well tested; Special handling for arrays/lists to ensure efficiency.

Install-Package Tedd.HashSetExtensions -Version 1.0.2
dotnet add package Tedd.HashSetExtensions --version 1.0.2
<PackageReference Include="Tedd.HashSetExtensions" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tedd.HashSetExtensions --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Tedd.HashSetExtensions, 1.0.2"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Tedd.HashSetExtensions as a Cake Addin
#addin nuget:?package=Tedd.HashSetExtensions&version=1.0.2

// Install Tedd.HashSetExtensions as a Cake Tool
#tool nuget:?package=Tedd.HashSetExtensions&version=1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Tedd.HashSetExtensions

.Net extension methods for ToHashSet<T>().<br />
All methods implemented with and without both selector and comparer. Special handling of Array and List to avoid enumeration overhead. In line with how ToDictionary is implemented in .Net source code.

Examples

All methods supports selector. A selector is used to pick or transform the object before it is added to the hash.

Selector example

var dic = new Dictionary<string, int>();
dic.Add("A", 1);
dic.Add("B", 2);

// A dictionary consists of KeyValuePair and we pick the Key portion of that for our HashSet
var hashSet = dic.ToHashSet(s => s.Key);

dic.Add("CC", 2);
// Or we can have more complex logic
var hashSet2 = dic.ToHashSet(s => {
	if (s.Key == "CC")
		return "C";
	return s.Key;
});

Methods

ienumerable.ToHashSet()

var list = new List<string>();
list.Add("A");
list.Add("B");
var hashSet = list.ToHashSet(); // No selector needed
var thisIsTrue = hashSet.Contains("A");
var thisIsFalse = hashSet.Contains("C");

// Add B again
list.Add("B");
var hashSet2 = list.ToHashSet(s => s); // Using selector
// HashSet only cointains 2 items because duplicates are ignored
var thisIsTwo = hashSet2.Count;

ienumerable.ToHashSet(selector, comparer)

var list = new List<string>();
list.Add("a");
list.Add("b");
var hashSet = list.ToHashSet(s => s, StringComparer.InvariantCultureIgnoreCase);
// HashSet now contains: a, b

var thisIsTrue = hashSet.Contains("a");
var thisIsAlsoTrue = hashSet.Contains("A");

hashset.ContainsRange(ienumerable)

var list = new List<string>();
list.Add("A");
list.Add("B");
list.Add("C");
var hashSet = list.ToHashSet(s => s);
// HashSet now contains: A, B, C

var otherList = new List<string>();
otherList.Add("A");
otherList.Add("B");

var thisIsTrue = hashSet.ContainsRange(otherList);

hashset.AddRange(ienumerable)

var list1 = new List<string>();
list1.Add("A");
list1.Add("B");
var hashSet = list1.ToHashSet(s => s);
// HashSet now contains: A, B

var list2 = new List<string>();
list.Add("C");
list.Add("D");
hashSet.AddRange(list2);
// HashSet now contains: A, B, C, D

var thisIsTrue = hashSet.Contains("D");

hashset.RemoveRange(ienumerable)

var list1 = new List<string>();
list1.Add("A");
list1.Add("B");
list1.Add("C");
list1.Add("D");
var hashSet = list1.ToHashSet();
// HashSet now contains: A, B, C, D

var list2 = new List<string>();
list.Add("A");
list.Add("B");
hashSet.RemoveRange(list2);
// HashSet now contains: B, C


var thisIsFalse = hashSet.Contains("A");
var thisIsAlsoFalse = hashSet.Contains("B");

Tedd.HashSetExtensions

.Net extension methods for ToHashSet<T>().<br />
All methods implemented with and without both selector and comparer. Special handling of Array and List to avoid enumeration overhead. In line with how ToDictionary is implemented in .Net source code.

Examples

All methods supports selector. A selector is used to pick or transform the object before it is added to the hash.

Selector example

var dic = new Dictionary<string, int>();
dic.Add("A", 1);
dic.Add("B", 2);

// A dictionary consists of KeyValuePair and we pick the Key portion of that for our HashSet
var hashSet = dic.ToHashSet(s => s.Key);

dic.Add("CC", 2);
// Or we can have more complex logic
var hashSet2 = dic.ToHashSet(s => {
	if (s.Key == "CC")
		return "C";
	return s.Key;
});

Methods

ienumerable.ToHashSet()

var list = new List<string>();
list.Add("A");
list.Add("B");
var hashSet = list.ToHashSet(); // No selector needed
var thisIsTrue = hashSet.Contains("A");
var thisIsFalse = hashSet.Contains("C");

// Add B again
list.Add("B");
var hashSet2 = list.ToHashSet(s => s); // Using selector
// HashSet only cointains 2 items because duplicates are ignored
var thisIsTwo = hashSet2.Count;

ienumerable.ToHashSet(selector, comparer)

var list = new List<string>();
list.Add("a");
list.Add("b");
var hashSet = list.ToHashSet(s => s, StringComparer.InvariantCultureIgnoreCase);
// HashSet now contains: a, b

var thisIsTrue = hashSet.Contains("a");
var thisIsAlsoTrue = hashSet.Contains("A");

hashset.ContainsRange(ienumerable)

var list = new List<string>();
list.Add("A");
list.Add("B");
list.Add("C");
var hashSet = list.ToHashSet(s => s);
// HashSet now contains: A, B, C

var otherList = new List<string>();
otherList.Add("A");
otherList.Add("B");

var thisIsTrue = hashSet.ContainsRange(otherList);

hashset.AddRange(ienumerable)

var list1 = new List<string>();
list1.Add("A");
list1.Add("B");
var hashSet = list1.ToHashSet(s => s);
// HashSet now contains: A, B

var list2 = new List<string>();
list.Add("C");
list.Add("D");
hashSet.AddRange(list2);
// HashSet now contains: A, B, C, D

var thisIsTrue = hashSet.Contains("D");

hashset.RemoveRange(ienumerable)

var list1 = new List<string>();
list1.Add("A");
list1.Add("B");
list1.Add("C");
list1.Add("D");
var hashSet = list1.ToHashSet();
// HashSet now contains: A, B, C, D

var list2 = new List<string>();
list.Add("A");
list.Add("B");
hashSet.RemoveRange(list2);
// HashSet now contains: B, C


var thisIsFalse = hashSet.Contains("A");
var thisIsAlsoFalse = hashSet.Contains("B");

Release Notes

Contains for array, list and ienumerable

  • .NETCoreApp 2.0

    • No dependencies.
  • .NETCoreApp 2.1

    • No dependencies.
  • .NETCoreApp 3.0

    • No dependencies.
  • .NETCoreApp 3.1

    • No dependencies.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETFramework 4.6.2

    • No dependencies.
  • .NETFramework 4.7

    • No dependencies.
  • .NETFramework 4.7.1

    • No dependencies.
  • .NETFramework 4.7.2

    • No dependencies.
  • .NETFramework 4.8

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.2 221 2/15/2020
1.0.1 216 2/14/2020
1.0.0 169 2/14/2020