Contract.Exists 如何增加价值?
我刚刚开始了解VS2010标配的代码契约库。我立即遇到的一件事是一些合同条款的真正含义。
例如,这两种说法有何不同?
Contract.Requires(!mycollection.Any(a => a.ID == newID));
Contract.Requires(!Contract.Exists(mycollection, a => a.ID == newID));
换句话说,Contract.Exists 在实际用途中做什么,无论是对于使用我的函数的开发人员,还是对于静态代码分析系统?
I am just starting to learn about the code contracts library that comes standard with VS2010. One thing I am running into right away is what some of the contract clauses really mean.
For example, how are these two statements different?
Contract.Requires(!mycollection.Any(a => a.ID == newID));
Contract.Requires(!Contract.Exists(mycollection, a => a.ID == newID));
In other words, what does Contract.Exists do in practical purposes, either for a developer using my function, or for the static code analysis system?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于其声明性,使用
Contract.Exists
的版本是首选。另一个优点是框架知道这个合约,并且更有可能在静态分析中“捕获”。The version that uses
Contract.Exists
is preferred due to its declarative nature. Another advantage is that the framework knows this contract and it has better chance of being "caught" in static analysis.好的,我找到了答案。根据《代码契约用户手册》第 2.7.2 节:
“也可以使用扩展方法 System.Linq.Enumerable.Any 而不是 Contract.Exists 。”
所以它们是等价的。我将使用 Any 而不是 Exists,因此它与我们代码的其余部分一致。
Ok, I found the answer. According to the Code Contracts User Manual, section 2.7.2:
"It is also possible to use the extension method System.Linq.Enumerable.Any instead of Contract.Exists ."
So they are equivalent. I will use Any instead of Exists, so it is consistent with the rest of our code.