-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathNullableHelper.cs
41 lines (38 loc) · 1.48 KB
/
NullableHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using JetBrains.Annotations;
namespace CodeJam
{
/// <summary>
/// Helper method for nullable types.
/// </summary>
[PublicAPI]
public static class NullableHelper
{
/// <summary>
/// Retrieves the value of the current <see cref="Nullable{T}"/> object, or value returned by factory.
/// </summary>
/// <typeparam name="T">The underlying value type of the <see cref="Nullable{T}"/> generic type.</typeparam>
/// <param name="value">Nullable value.</param>
/// <param name="defaultFactory">
/// A function to return default value if the <see cref="Nullable{T}.HasValue"/> property is <c>false</c>.
/// </param>
/// <returns>
/// The value of the <see cref="Nullable{T}.Value"/> property if the <see cref="Nullable{T}.HasValue"/> property is
/// <c>true</c>; otherwise, the value returned by <paramref name="defaultFactory"/> parameter.
/// </returns>
[Pure, System.Diagnostics.Contracts.Pure]
public static T GetValueOrDefault<T>(T? value, [InstantHandle] Func<T> defaultFactory) where T : struct
{
Code.NotNull(defaultFactory, nameof(defaultFactory));
return value ?? defaultFactory();
}
/// <summary>
/// Returns nullable of specified value.
/// </summary>
/// <typeparam name="T">Type of value</typeparam>
/// <param name="value">THe value</param>
/// <returns><paramref name="value"/> wrapped in nullable.</returns>
[Pure, System.Diagnostics.Contracts.Pure]
public static T? AsNullable<T>(this T value) where T : struct => value;
}
}