什么时候应该使用 C# 4.0 附带的代码合约?
我正在解决一个关于 SO 的问题,该问题是关于 c# 4.0 的新功能 和 jon skeet 的答案有 C# 4.0 的代码契约功能..但我真的不明白何时使用它们..有什么建议吗...
I was going through a question on SO which was about new features of c# 4.0 and jon skeet's answer had Code Contracts feature of C# 4.0.. But i really cant understand when to use them.. Any suggestion...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要有可能。例如,在方法开头使用保护子句的任何地方,
您都应该使用
Contract
的优点在于,它们会公开,并且可以成为文档的一部分,而无需额外的操作。就你而言,因为保护条款不是公开的,只能通过一些繁重的工作才能放入文档中。因此,通过Contract
,您可以在代码中更清晰地表达需求和承诺。Whenever possible. For example, anywhere that you would use a guard clause at the beginning of a method like
you should instead use
What is beautiful about
Contract
is that they become public and can be made part of the documentation with no additional work on your part where as the guard clause were not public and could only be put into documentation with some heavy lifting. Therefore, withContract
you can more clearly express requirements and promises in your code.合约的真正美妙之处在于,您不必硬连线那些丑陋的参数名称字符串......
what's really beautiful with contracts is that you don't have to hard wire those ugly argument name strings...