11Function New-HashSet () {
2+ <#
3+ . SYNOPSIS
4+ Creates a HashSet of unique values.
5+
6+ . DESCRIPTION
7+ Creates a 'System.Collections.Generic.HashSet[T]' in order to store unique values into.
8+
9+ . PARAMETER Capacity
10+ The total number of elements the set can hold without resizing. Default -- 0.
11+
12+ . PARAMETER GenericType
13+ The constraining type that every object added into the list must be.
14+
15+ . PARAMETER EqualityScript
16+ The scripblock that will check the equality between any 2 objects in the set. It must return a boolean (True/False) value.
17+
18+ '$x' -or- '$args[0]' must represent the 1st item to be compared.
19+ '$y' -or - '$args[1]' must represent the 2nd item to be compared.
20+
21+ . PARAMETER HashCodeScript
22+ The scriptblock that retrieve an item's hash code value.
23+
24+ A "hash code" is a numeric value that is used to insert and identify an object in a "hash-based" collection.
25+ The easiest way to provide this through an object's 'GetHashCode()' method.
26+ Two objects that are equal return hash codes that are equal. However, the reverse is not true: equal hash codes
27+ do not imply object equality, becuase different (unequal) objects can have identical hash codes.
28+
29+ . INPUTS
30+ System.Object[] -- The objects that will immediately added to the returned set.
31+
32+ . OUTPUTS
33+ System.Collections.Generic.HashSet[T] -- where 'T' is the constrained generic type that all objects must be.
34+
35+ . EXAMPLE
36+ # Create a HashSet[string] that ignores case for equality.
37+ $set = New-HashSet [string] -EqualityScript { $x -eq $y } -HashCodeScript { $_.ToLower().GetHashCode() }
38+
39+ . EXAMPLE
40+ # Create a HashSet[object] that objects with the same 'Name' and 'Id' properties equal.
41+ $set = New-HashSet -EqualityScript { $x.Name -eq $y.Name -and $x.Id -eq $y.Id }
42+
43+ . NOTES
44+ The EqualityScript must use either '$x' and '$y' -or- '$args[0]' and '$args[1]' in the
45+ scriptblock to properly identify the 2 comparing values.
46+
47+ The HashCodeScript must use either '$_' -or- '$args[0]' in the scriptblock to properly identify
48+ the object whose hash code is retrieved.
49+ #>
250
351 [CmdletBinding (DefaultParameterSetName = " None" )]
452 param (
@@ -18,8 +66,8 @@ Function New-HashSet() {
1866 [Parameter (Mandatory = $true , ParameterSetName = " WithCustomEqualityComparer" )]
1967 [scriptblock ] $EqualityScript ,
2068
21- [Parameter (Mandatory = $true , ParameterSetName = " WithCustomEqualityComparer" )]
22- [scriptblock ] $HashCodeScript
69+ [Parameter (Mandatory = $false , ParameterSetName = " WithCustomEqualityComparer" )]
70+ [scriptblock ] $HashCodeScript = { $_ .GetHashCode () }
2371 )
2472 Begin {
2573
0 commit comments