有没有办法要求提供给方法的参数不为空?

Eri*_*tas 5 c# methods null arguments

是否有更好的方法要求方法中的参数不为null?我一直在检查我的方法所需的任何参数是否为null,如下所示。但是我想知道是否有更好的方法。

public void MyMethod(string a, int b)
{
   if(a==null){throw new ArgumentNullException("a");}
   if(b==null){throw new ArgumentNullException("b");}

   //more stuff here
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*eck 3

Rick Brewster(Paint.NET 的作者)在博客中介绍了 Fluent API 的替代方案:

http://blog.getpaint.net/2008/12/06/a- Fluent-approach-to-c-parameter-validation/