/
GenericExtensions.cs
38 lines (35 loc) · 1.5 KB
/
GenericExtensions.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
using System;
namespace AglCodingTest.Extensions
{
/// <summary>
/// This represents the extension entity for generic.
/// </summary>
public static class GenericExtensions
{
/// <summary>
/// Checks whether the given instance is <c>null</c> or empty.
/// </summary>
/// <typeparam name="T">Type of instance.</typeparam>
/// <param name="instance">Instance to check.</param>
/// <returns>Returns <c>True</c>, if the original instance is <c>null</c> or empty; otherwise returns <c>False</c>.</returns>
public static bool IsNullOrDefault<T>(this T instance)
{
return instance == null || instance.Equals(default(T));
}
/// <summary>
/// Throws an <see cref="ArgumentNullException"/> if the given instance is <c>null</c> or empty.
/// </summary>
/// <typeparam name="T">Type of instance.</typeparam>
/// <param name="instance">Instance to check.</param>
/// <returns>Returns the original instance, if the instance is NOT <c>null</c>; otherwise throws an <see cref="ArgumentNullException"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="instance"/> is <see langword="null"/></exception>
public static T ThrowIfNullOrDefault<T>(this T instance)
{
if (instance.IsNullOrDefault())
{
throw new ArgumentNullException(nameof(instance));
}
return instance;
}
}
}