Skip to content

Instantly share code, notes, and snippets.

@johnlcox
Created February 25, 2013 19:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnlcox/5032524 to your computer and use it in GitHub Desktop.
Save johnlcox/5032524 to your computer and use it in GitHub Desktop.
C# Preconditions
public class Preconditions
{
/// <summary>
/// Ensures that an object reference passed as a parameter to the calling method is not null.
/// </summary>
/// <param name="reference">an object reference</param>
/// <param name="errorMessage">the exception message to use if the check fails</param>
/// <returns>the non-null reference that was validated</returns>
/// <exception cref="NullReferenceException">if reference is null</exception>
public static T CheckNotNull<T>(T reference, string errorMessage)
{
if (reference == null)
{
throw new NullReferenceException(errorMessage);
}
return reference;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment