Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Nullable<T>.TryGetValue (resolves #61)
- Loading branch information
1 parent
c6849a0
commit d9cf9c8
Showing
3 changed files
with
60 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using X10D.Core; | ||
|
||
namespace X10D.Tests.Core; | ||
|
||
[TestClass] | ||
public class NullableTests | ||
{ | ||
[TestMethod] | ||
public void TryGetValue_ShouldBeTrue_GivenValue() | ||
{ | ||
int? value = 42; | ||
Assert.IsTrue(value.TryGetValue(out int returnedValue)); | ||
Assert.AreEqual(value, returnedValue); | ||
} | ||
|
||
[TestMethod] | ||
public void TryGetValue_ShouldBeFalse_GivenNull() | ||
{ | ||
int? value = null; | ||
Assert.IsFalse(value.TryGetValue(out int returnedValue)); | ||
Assert.AreEqual(default, returnedValue); | ||
} | ||
} |
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,35 @@ | ||
namespace X10D.Core; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="Nullable{T}" /> | ||
/// </summary> | ||
public static class NullableExtensions | ||
{ | ||
/// <summary> | ||
/// Attempts to get the value of a <see cref="Nullable{T}" />, and returns a value indicating the success of the | ||
/// operation. | ||
/// </summary> | ||
/// <param name="value">The nullable value.</param> | ||
/// <param name="result"> | ||
/// When this method returns, contains the result of <see cref="Nullable{T}.Value" />, if | ||
/// <see cref="Nullable{T}.HasValue" /> is <see langword="true" />; otherwise, returns the default value for | ||
/// <typeparamref name="T" />. | ||
/// </param> | ||
/// <typeparam name="T">The type of the value.</typeparam> | ||
/// <returns> | ||
/// <see langword="true" /> if the value's <see cref="Nullable{T}.HasValue" /> is <see langword="true" />; otherwise, | ||
/// <see langword="false" />. | ||
/// </returns> | ||
public static bool TryGetValue<T>(this T? value, out T result) | ||
where T : struct | ||
{ | ||
if (value.HasValue) | ||
{ | ||
result = value.Value; | ||
return true; | ||
} | ||
|
||
result = default; | ||
return false; | ||
} | ||
} |