.NET 4.0 中代码契约的实际用途是什么?
为了充分理解和利用新的 .NET Framework 4.0 提供的新功能和增强功能,我想获取 真实世界应用程序示例“http://msdn.microsoft.com/en-us/library/system.diagnostics.contracts.aspx”rel="noreferrer">代码合同。
- 有人有应用此功能的好示例吗?
我想要一个带有简短说明的代码示例,以帮助我启动并运行它。
In order to fully understand and take advantage of the new features and enhancements provided with the coming of the new .NET Framework 4.0, I would like to get an example of real-world application of Code Contracts.
- Anyone has a good example of application of this feature?
I would like to get a code sample with a brief explanation to help me get up and running with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自代码契约用户手册< /a>:
代码合约用于静态验证;想象一下,如果在编译时,您不仅发现了语法错误,还发现了逻辑错误。这就是静态程序验证的愿景。
真实世界示例
您可以使用合约(和静态验证)来降低测试成本……特别是回归测试。举个例子,假设我编写了一些代码来满足一些业务需求......但后来,性能需要改变,我需要进行优化。如果我首先编写合同,那么当我的新优化代码得到验证时,如果它不再满足原始合同,我将在 IDE 中收到一条错误消息,就像我遇到编译时错误一样。因此,您几乎可以立即发现并解决错误,这比一轮测试的成本还要少。
From The Code Contracts User Manual:
Code Contracts are used for static verification; imagine if - at compile time - you caught not only syntax errors but also logic errors. This is the vision of static program verification.
Real World Example
You could use contracts (and static verification) to reduce the cost of testing... in particular regression testing. As an example, let's say I write some code which fulfills some business needs... but later, performance needs change, and I'm required to optimize. If I first write a contract, then - when my new optimized code is verified - if it no longer fulfills the original contract I'll get an error message in my IDE, just like if I had a compile time error. As a result, you find and resolve the bug almost immediately, which costs less than a round of testing.
在即将出版的书中有一个关于代码契约的免费章节C# 深入探讨,第二版。一个叫 Jon Skeet 的人,你们中的一些人可能很熟悉他:)
至于实际用途。您可以在代码中的任何位置使用它们,但特别是如果您正在开发很多人都会使用的框架/API 类型库,我希望它们会非常方便。与在运行时发现您没有处理某些边缘情况相比,对代码进行静态验证可以节省大量时间。
您可以随心所欲地记录您的方法用法,但人们真的会阅读该文档吗?方法 y 中的字符串参数 x 是否允许为空?代码合约可以提供该信息,从而消除猜测。
下面是这种情况的一个示例:
如果有人尝试将可能为 null 的字符串传递给 CountWhitespace,验证将会发出警告。此外,它还会在运行时抛出ArgumentNullException。
我最近才将我的私有类库转换为 .NET 4.0,但我计划很快向其中添加代码契约。
There is a freely available chapter about code contracts in the upcoming book C# in Depth, second edition. By some guy named Jon Skeet, some of you may be familiar with him :)
As for practical usage. You can use them anywhere in your code, but especially if you're developing framework / API type libraries that lots of people will be using, I expect them to come in quite handy. Static verification of your code saves a lot of time compared to finding out at runtime that you didn't handle some edge case.
You may document your method usage all you like, but will people actually read that documentation? Is it allowed to have string parameter x in method y be null, or not? Code contracts can provide that information, to take the guesswork out of the equation.
Here's an example of just such a case:
The verification will complain if anyone tried to pass a string to
CountWhitespace
that might be null. In addition, it will throw an ArgumentNullException at runtime.I've only recently converted my private class library to .NET 4.0, but I plan to add code contracts to it real soon.
.Net 中有很多地方使用了契约。
>>来源<<
There is a lot of places where contracts are used in .Net.
>>Sources<<
您是否曾经见过 NullReferenceException,并希望编译器能够在编译时向您发出警告,以避免发现困难 - 程序崩溃?
使用代码契约,您可以编写如下内容:
这不仅仅是运行时检查 - 您可以对其进行设置,以便如果您使用可能为 null 的参数调用此函数,您将收到编译错误。
这是一个现实世界的例子:
Have you ever seen a
NullReferenceException
and wished that the compiler could have warned you about it at compile time to avoid finding out the hard way - with your program crashing?With code contracts you can write things like:
This isn't just a runtime check - you can set it up so that if you call this function with an argument that might be null you will get a compile error.
Here's a real world example: