本文介绍了按合同设计/ C#4.0 /避免ArgumentNullException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据我的理解,C#4.0使得一些设计可以通过合同来实现结构体。是否可以指定一个方法不会接受C#4.0中的空参数?
此外,在这段时间内可以做什么(也许是属性?)
解决方案
您可以创建一个 NotNull< T> ;
通用类有帮助,但有一些副作用。请参阅。 p>
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?
解决方案
You can create a NotNull<T>
generic class that helps, but there are some side effects. See Robert Nystrom's blog post.
这篇关于按合同设计/ C#4.0 /避免ArgumentNullException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!