Monday, January 30, 2012

Best Extension Methods: Throw exception argument is Null

This extension method is for parameters of function where you wants that if a parameter/argument of a function is null then you want an exception to be thrown.
//Throw exception if parameters of function is null
    // eg: public Test(string input1)
    //{
    //    input1.ThrowIfArgumentIsNull("input1");
    //}
    public static void ThrowIfArgumentIsNull(this T obj, string parameterName) where T : class
    {
        if (obj == null) throw new ArgumentNullException(parameterName + " not allowed to be null");
    }

Compiled By: Rajesh Rolen

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates