Skip to content

Commit

Permalink
KeyHelper: Add null checks on arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Emik03 committed Jul 11, 2021
1 parent d52ad49 commit 97a534d
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions Source/Helpers/KeyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static class KeyHelper
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <param name="action">The action to try.</param>
/// <param name="caught">The action to run when an exception is caught.</param>
/// <param name="final">The action to run on either clause.</param>
Expand All @@ -55,6 +56,7 @@ public static void Catch(this Action action, Action<Exception> caught = null, Ac
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <typeparam name="T">The type to catch.</typeparam>
/// <param name="action">The action to try.</param>
/// <param name="caught">The action to run when an exception is caught.</param>
Expand All @@ -67,6 +69,7 @@ public static void Catch(this Action action, Action<Exception> caught = null, Ac
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <typeparam name="T1">The first type to catch.</typeparam>
/// <typeparam name="T2">The second type to catch.</typeparam>
/// <param name="action">The action to try.</param>
Expand All @@ -80,6 +83,7 @@ public static void Catch(this Action action, Action<Exception> caught = null, Ac
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <typeparam name="T1">The first type to catch.</typeparam>
/// <typeparam name="T2">The second type to catch.</typeparam>
/// <typeparam name="T3">The third type to catch.</typeparam>
Expand All @@ -94,6 +98,7 @@ public static void Catch(this Action action, Action<Exception> caught = null, Ac
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <typeparam name="T1">The first type to catch.</typeparam>
/// <typeparam name="T2">The second type to catch.</typeparam>
/// <typeparam name="T3">The third type to catch.</typeparam>
Expand All @@ -109,6 +114,7 @@ public static void Catch(this Action action, Action<Exception> caught = null, Ac
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <typeparam name="T">The return type.</typeparam>
/// <param name="action">The action to try.</param>
/// <param name="caught">The action to run when an exception is caught.</param>
Expand All @@ -131,6 +137,7 @@ public static T Catch<T>(this Func<T> action, Func<Exception, T> caught, params
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <typeparam name="T">The type to catch.</typeparam>
/// <typeparam name="TResult">The return type.</typeparam>
/// <param name="action">The action to try.</param>
Expand All @@ -143,6 +150,7 @@ public static T Catch<T>(this Func<T> action, Func<Exception, T> caught, params
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <typeparam name="T1">The first type to catch.</typeparam>
/// <typeparam name="T2">The second type to catch.</typeparam>
/// <typeparam name="TResult">The return type.</typeparam>
Expand All @@ -156,6 +164,7 @@ public static T Catch<T>(this Func<T> action, Func<Exception, T> caught, params
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <typeparam name="T1">The first type to catch.</typeparam>
/// <typeparam name="T2">The second type to catch.</typeparam>
/// <typeparam name="T3">The third type to catch.</typeparam>
Expand All @@ -170,6 +179,7 @@ public static T Catch<T>(this Func<T> action, Func<Exception, T> caught, params
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <typeparam name="T1">The first type to catch.</typeparam>
/// <typeparam name="T2">The second type to catch.</typeparam>
/// <typeparam name="T3">The third type to catch.</typeparam>
Expand All @@ -185,6 +195,7 @@ public static T Catch<T>(this Func<T> action, Func<Exception, T> caught, params
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/checked"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <param name="action">The action to run inside a <see langword="checked"/> block.</param>
public static void Checked(this Action action)
{
Expand All @@ -200,6 +211,7 @@ public static void Checked(this Action action)
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-do-statement"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <param name="action">The action to run in the loop.</param>
/// <param name="condition">The condition to determine whether the loop should keep going.</param>
public static void DoWhile(this Action action, Func<bool> condition)
Expand All @@ -214,6 +226,7 @@ public static void DoWhile(this Action action, Func<bool> condition)
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-for-statement"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <typeparam name="T">The type of the declaring variable.</typeparam>
/// <param name="item">The item to read and write on.</param>
/// <param name="action">The action for each loop.</param>
Expand All @@ -238,16 +251,18 @@ public static void For<T>(this T item, Action<T> action, Predicate<T> condition
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-foreach-statement"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <param name="iterator">The collection of items to go through one-by-one.</param>
/// <param name="action">The action to do on each item in <paramref name="iterator"/>.</param>
public static void ForEach(this IEnumerable iterator, Action<object> action) => ForEach(iterator.Cast<object>(), action);
public static void ForEach(this IEnumerable iterator, Action<object> action) => ForEach(iterator.NullCheck("The iterator cannot be null.").Cast<object>(), action);

/// <summary>
/// The <see langword="foreach"/> statement executes a statement or a block of statements for each element in an instance of the type that implements the <see cref="IEnumerable"/> or <see cref="IEnumerable{T}"/> interface.
/// </summary>
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-foreach-statement"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <typeparam name="T">The type of iterator.</typeparam>
/// <param name="iterator">The collection of items to go through one-by-one.</param>
/// <param name="action">The action to do on each item in <paramref name="iterator"/>.</param>
Expand All @@ -265,6 +280,7 @@ public static void ForEach<T>(this IEnumerable<T> iterator, Action<T> action)
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-foreach-statement"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <param name="iterator">The collection of items to go through one-by-one.</param>
/// <param name="action">The action to do on each item in <paramref name="iterator"/>.</param>
public static void ForEach(this IEnumerator iterator, Action<object> action) => ForEach(iterator.AsEnumerable(), action);
Expand All @@ -275,6 +291,7 @@ public static void ForEach<T>(this IEnumerable<T> iterator, Action<T> action)
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-foreach-statement"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <typeparam name="T">The type of iterator.</typeparam>
/// <param name="iterator">The collection of items to go through one-by-one.</param>
/// <param name="action">The action to do on each item in <paramref name="iterator"/>.</param>
Expand All @@ -283,14 +300,19 @@ public static void ForEach<T>(this IEnumerable<T> iterator, Action<T> action)
/// <summary>
/// An <see langword="if"/> statement identifies which statement to run based on the value of a Boolean expression.
/// </summary>
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <param name="condition">The condition to check.</param>
/// <param name="action">The action to run when <paramref name="condition"/> is <see langword="true"/>.</param>
/// <param name="otherwise">The action to run when <paramref name="condition"/> is <see langword="false"/>.</param>
public static void If(this bool condition, Action action, Action otherwise = null)
{
action.NullCheck("The action cannot be null.");

if (condition)
action?.Invoke();
action();
else
otherwise?.Invoke();
}
Expand All @@ -315,7 +337,7 @@ public static void If(this bool condition, Action action, Action otherwise = nul
/// <typeparam name="T">The type to cast into.</typeparam>
/// <param name="obj">The object to cast.</param>
/// <param name="item">The object casted into the type.</param>
/// <returns><paramref name="obj"/> <see langword="is"/> <typeparamref name="T"/></returns>
/// <returns><paramref name="obj"/> <see langword="is"/> <typeparamref name="T"/> <paramref name="item"/></returns>
public static bool Is<T>(this object obj, out T item) where T : class => (item = obj as T) is T;

/// <summary>
Expand All @@ -324,6 +346,7 @@ public static void If(this bool condition, Action action, Action otherwise = nul
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/lock-statement"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <typeparam name="T">The type of item to lock.</typeparam>
/// <param name="item">The item to lock.</param>
/// <param name="action">The action to run while the item is locked.</param>
Expand Down Expand Up @@ -362,6 +385,7 @@ public static string NameOf<T>(this Expression<Func<T>> expression)
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/unchecked"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <param name="action">The action to ignore overflow-checking.</param>
public static void Unchecked(this Action action)
{
Expand All @@ -377,6 +401,7 @@ public static void Unchecked(this Action action)
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <typeparam name="T">The type of <see cref="IDisposable"/>.</typeparam>
/// <param name="item">The item to use.</param>
/// <param name="action">The action to use <paramref name="item"/> on.</param>
Expand All @@ -392,6 +417,7 @@ public static void Unchecked(this Action action)
/// <remarks>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-while-statement"/>
/// </remarks>
/// <exception cref="NullReferenceException"></exception>
/// <param name="action">The action to run in the loop.</param>
/// <param name="condition">The condition to determine whether the loop should keep going.</param>
public static void While(this Action action, Func<bool> condition)
Expand Down

0 comments on commit 97a534d

Please sign in to comment.