Skip to content

Instantly share code, notes, and snippets.

@MioGreen
Created November 14, 2011 09:39
Show Gist options
  • Save MioGreen/1363616 to your computer and use it in GitHub Desktop.
Save MioGreen/1363616 to your computer and use it in GitHub Desktop.
Requires<T> pattern
public static class Contract
{
public static void Requires<T>(Func<bool> condition) where T : Exception, new()
{
if(!condition.Invoke())
{
throw new T();
}
}
}
#Example of using
public class A
{
public A(params IB[] bs)
{
Contract.Requires<ArgumentException>(() => bs != null);
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment