Search code examples
c#-4.0argumentnullexception

Design by contract/C# 4.0/avoiding ArgumentNullException


I'm terribly tired of checking all my arguments for null, and throwing ArgumenutNullExceptions when they are.

As I understand it, C# 4.0 enables some design by contract constructs. Will it be possible to specify that a method will not accept null arguments in C# 4.0?

Also, is there anything I can do in the meantime (maybe an attribute?) to avoid this monotonous task of checking for null and throwing?


Solution

  • You can create a NotNull<T> generic class that helps, but there are some side effects. See Robert Nystrom's blog post.