-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
191ea7b
commit e1f0786
Showing
5 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// <auto-generated /> | ||
|
||
#pragma warning disable | ||
|
||
#if NET46X || NET47 || NET471 || NETSTANDARD2_0 | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Link = System.ComponentModel.DescriptionAttribute; | ||
|
||
static partial class Polyfill | ||
{ | ||
/// <summary> | ||
/// Searches the set for a given value and returns the equal value it finds, if any. | ||
/// </summary> | ||
/// <param name="equalValue">The value to search for.</param> | ||
/// <param name="actualValue">The value from the set that the search found, or the default value of T when the search yielded no match.</param> | ||
/// <returns>A value indicating whether the search was successful.</returns> | ||
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.trygetvalue")] | ||
public static bool TryGetValue<T>( | ||
this HashSet<T> target, | ||
T equalValue, | ||
[MaybeNullWhen(false)] out T actualValue) | ||
{ | ||
var comparer = target.Comparer; | ||
var hashCode = comparer.GetHashCode(equalValue); | ||
foreach (var item in target) | ||
{ | ||
if (comparer.GetHashCode(item) == hashCode && | ||
comparer.Equals(item, equalValue)) | ||
{ | ||
actualValue = item; | ||
return true; | ||
} | ||
} | ||
|
||
actualValue = default; | ||
return false; | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
partial class PolyfillTests | ||
{ | ||
[Test] | ||
public void TryGetValue() | ||
{ | ||
var value = "value"; | ||
var set = new HashSet<string> | ||
{ | ||
value | ||
}; | ||
var found = set.TryGetValue("value", out var result); | ||
Assert.True(found); | ||
Assert.AreSame(value, result!); | ||
found = set.TryGetValue("value2", out result); | ||
Assert.Null(result); | ||
Assert.False(found); | ||
} | ||
} |