-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathGuard.cs
32 lines (28 loc) · 970 Bytes
/
Guard.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
using System.Runtime.CompilerServices;
namespace Anet;
public static class Guard
{
public static void NotNull(object argument, [CallerArgumentExpression(nameof(argument))] string paramName = null)
{
if (argument is null)
{
throw new ArgumentNullException(paramName);
}
}
public static void NotNullOrEmpty(string argument, [CallerArgumentExpression(nameof(argument))] string paramName = null)
{
NotNull(argument, paramName);
if (argument == string.Empty)
{
throw new ArgumentException("The argument can not be empty.", paramName);
}
}
public static void NotNullOrEmpty<T>(IEnumerable<T> argument, [CallerArgumentExpression(nameof(argument))] string paramName = null)
{
NotNull(argument, paramName);
if (!argument.Any())
{
throw new ArgumentException("The collection can not be empty.", paramName);
}
}
}