有没有办法要求提供给方法的参数不为空? [英] Is there a way to require that an argument provided to a method is not null?
本文介绍了有没有办法要求提供给方法的参数不为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有更好的方法来要求方法中的参数不为空?我一直在检查我的方法需要的任何参数是否为空,如下所示.但我想知道是否有更好的方法.
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(Paint.NET 的作者)在博客中介绍了 Fluent API 替代方案:
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屋!
查看全文