/
NullableHelper.cs
98 lines (92 loc) · 4.02 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
namespace MouseRx2Wpf
{
/// <summary>
/// Provides a set of methods for nullable objects (class or <see cref="Nullable{T}"/>).
/// </summary>
public static class NullableHelper
{
/// <summary>
/// Does an action for an object, if the object is not null.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <param name="obj">An object.</param>
/// <param name="action">An action.</param>
/// <returns>The original object.</returns>
public static T IfNotNull<T>(this T obj, Action<T> action)
where T : class
{
if (obj != null) action(obj);
return obj;
}
/// <summary>
/// Does an action for an object, if the object is not null.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <param name="obj">An object.</param>
/// <param name="action">An action.</param>
/// <returns>The original object.</returns>
public static T? IfNotNull<T>(this T? obj, Action<T> action)
where T : struct
{
if (obj.HasValue) action(obj.Value);
return obj;
}
/// <summary>
/// Passes an object to a function, if the object is not null.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <typeparam name="TResult">The type of the result object.</typeparam>
/// <param name="obj">An object.</param>
/// <param name="func">A function.</param>
/// <returns>The return value of the function if the object is not null; otherwise, null.</returns>
public static TResult IfNotNull<T, TResult>(this T obj, Func<T, TResult> func)
where T : class
where TResult : class
{
return obj != null ? func(obj) : default(TResult);
}
/// <summary>
/// Passes an object to a function, if the object is not null.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <typeparam name="TResult">The type of the result object.</typeparam>
/// <param name="obj">An object.</param>
/// <param name="func">A function.</param>
/// <returns>The return value of the function if the object is not null; otherwise, null.</returns>
public static TResult IfNotNull<T, TResult>(this T? obj, Func<T, TResult> func)
where T : struct
where TResult : class
{
return obj.HasValue ? func(obj.Value) : default(TResult);
}
/// <summary>
/// Passes an object to a function, if the object is not null.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <typeparam name="TResult">The type of the result object.</typeparam>
/// <param name="obj">An object.</param>
/// <param name="func">A function.</param>
/// <returns>The return value of the function if the object is not null; otherwise, null.</returns>
public static TResult? IfNotNull2<T, TResult>(this T obj, Func<T, TResult> func)
where T : class
where TResult : struct
{
return obj != null ? func(obj) : default(TResult?);
}
/// <summary>
/// Passes an object to a function, if the object is not null.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <typeparam name="TResult">The type of the result object.</typeparam>
/// <param name="obj">An object.</param>
/// <param name="func">A function.</param>
/// <returns>The return value of the function if the object is not null; otherwise, null.</returns>
public static TResult? IfNotNull2<T, TResult>(this T? obj, Func<T, TResult> func)
where T : struct
where TResult : struct
{
return obj.HasValue ? func(obj.Value) : default(TResult?);
}
}
}