是否有需要提供给方法的参数是不是空的方法吗? [英] Is there a way to require that an argument provided to a method is not null?
本文介绍了是否有需要提供给方法的参数是不是空的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有需要的参数不是null的方法更好的办法?我继续检查是否有任何我的方法需要的参数为空,如下秀。但我不知道是否有更好的方法。
公共无效的MyMethod(字符串,INT B)
{
如果(A == NULL){抛出新的ArgumentNullException(A);}
如果(二== NULL){抛出新的ArgumentNullException(B);} //更多的东西在这里
}
解决方案
里克·布鲁斯特(Paint.NET的作者)的博客上讲述一个流利的API的选择:
的http://blog.getpaint.net/2008/12/06/a-fluent-approach-to-c-parameter-validation/
Is there a better way to require that an argument is not null in a method? I keep checking if any of the arguments that my method requires are null, as show below. But I'm wondering if there is a better way.
public void MyMethod(string a, int b)
{
if(a==null){throw new ArgumentNullException("a");}
if(b==null){throw new ArgumentNullException("b");}
//more stuff here
}
解决方案
Rick Brewster (author of Paint.NET) blogged about a Fluent API alternative:
http://blog.getpaint.net/2008/12/06/a-fluent-approach-to-c-parameter-validation/
这篇关于是否有需要提供给方法的参数是不是空的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文