code-contracts

.net 4.0 Code Contracts. When to use? When are they a waste of time?

巧了我就是萌 提交于 2019-11-29 13:34:55
问题 I have been studying .NET 4.0 Code Contracts and looking on stackoverflow as well at question regarding this. I still have never come across any sample code that uses code contracts so that gets me wondering.. is this really useful ? Or maybe its only useful one your code reaches a certain complexity? Anyone out there using Code Contracts and really glad they did? Seems to me that all Code Contracts are is a Assertion on what goes in and what goes out of a method with the addition of being

Building with Code Contracts?

折月煮酒 提交于 2019-11-29 11:50:12
问题 I have the following method: private void DoSomething(CoolClass coolClass) { if (coolClass == null) { throw new ArgumentNullException("coolClass"); } coolClass.Name = "Pepe"; } With Code Contracts we can write it like this: private void DoSomething(CoolClass coolClass) { Contract.Requires<ArgumentNullException>(coolClass != null, "IS NULLL!"); coolClass.Name = "Pepe"; } The second method is shorter and simpler. The problem that I have is that when you build it, in runtime it does not throw

Design by contracts and constructors

青春壹個敷衍的年華 提交于 2019-11-29 06:30:46
I am implementing my own ArrayList for school purposes, but to spice up things a bit I'm trying to use C# 4.0 Code Contracts. All was fine until I needed to add Contracts to the constructors. Should I add Contract.Ensures() in the empty parameter constructor? public ArrayList(int capacity) { Contract.Requires(capacity > 0); Contract.Ensures(Size == capacity); _array = new T[capacity]; } public ArrayList() : this(32) { Contract.Ensures(Size == 32); } I'd say yes, each method should have a well defined contract. On the other hand, why put it if it's just delegating work to the "main" constructor

Why .net exception is not caught?

♀尐吖头ヾ 提交于 2019-11-29 06:29:55
Consider the following "Safe" program: internal class Safe { public static void SafeMethodWillNeverThrow() { try { var something = ThrowsNewException(); Func<int, string> x = p => something.ToString(); } catch (Exception) { } } private static object ThrowsNewException() { throw new Exception(); } public static void Main() { SafeMethodWillNeverThrow(); } } It should never complete with an exception. But why it fails when I run it? Why SafeMethodWillNeverThrow() throws the Exception? Before testing this code please read the answer below. It is because you have Code Contracts Runtime Contract

Code Contracts [Type]implements interface method {Interface.Method} thus cannot add requires

非 Y 不嫁゛ 提交于 2019-11-29 05:31:33
I have the following scenario: public interface ISomething { void DoStuff(); //... } public class Something : ISomething { private readonly ISomethingElse _somethingElse; //... public Something (ISomethingElse somethingElse) { Contract.Requires(somethingElse != null); _somethingElse = somethingElse; } public void DoStuff() { // *1* Please look at explanation / question below _somethingElse.DoThings(); } } At line 1 and with the static checker on, I'll get a warning saying that _somethingElse is possibly null, and if I add a contract it will give me the error [Type]implements interface method

.NET 4.0 code contracts - How will they affect unit testing?

人走茶凉 提交于 2019-11-29 04:38:28
问题 For example this article introduces them. What is the benefit? Static analysis seems cool but at the same time it would prevent the ability to pass null as a parameter in unit test. (if you followed the example in the article that is) While on the topic of unit testing - given how things are now surely there is no point for code contracts if you already practice automated testing? Update Having played with Code Contracts I'm a little disappointed. For example, based on the code in the

Why is ccrewrite.exe not doing anything from the command line?

拟墨画扇 提交于 2019-11-29 02:54:50
I've got Code Contracts working fine from inside Visual Studio 2010, but I can't get ccrewrite.exe to do anything useful from the command line. Here's a sample app: using System.Diagnostics.Contracts; public class Dummy { public static void Main(string[] args) { Contract.Requires(args.Length > 0); } } I then compile the code and run ccrewrite.exe on it: > csc /debug+ /D:CONTRACTS_FULL Dummy.cs > ccrewrite /o:RewrittenDummy.exe Dummy.exe elapsed time: 61ms There's no RewrittenDummy.exe file afterwards. I've tried loads of options, but nothing's making any difference. A few things I've noticed:

.Net Code Contracts - Where to learn more? [closed]

扶醉桌前 提交于 2019-11-29 02:39:15
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I had overheard some discussion in my office recently about .Net "Contracts" however, when I asked some of my fellow employees, not of them could easily explain to me what they were for, or what they even were. Does anyone have any resources, explanations, and perhaps a tutorial on their usage? Thanks, Paul 回答1:

C#: Code Contracts vs. normal parameter validation

北战南征 提交于 2019-11-29 00:27:24
问题 consider the following two pieces of code: public static Time Parse(string value) { string regXExpres = "^([0-9]|[0-1][0-9]|2[0-3]):([0-9]|[0-5][0-9])$|^24:(0|00)$"; Contract.Requires(value != null); Contract.Requires(new Regex(regXExpres).IsMatch(value)); string[] tokens = value.Split(':'); int hour = Convert.ToInt32(tokens[0], CultureInfo.InvariantCulture); int minute = Convert.ToInt32(tokens[1], CultureInfo.InvariantCulture); return new Time(hour, minute); } and public static Time Parse

Throwing an exception vs Contract.Requires<T>?

做~自己de王妃 提交于 2019-11-28 20:35:35
I'm wondering whether should I throw exceptions or call Contract.Requires<TException> For example: public static void Function(String str) { if (str == null) throw new ArgumentNullException("str", "Input string cannot be null."); // ... } vs public static void Function(String str) { Contract.Requires<ArgumentNullException>(str != null, "Input string cannot be null."); // ... } Since Contract.Requires<TException> doesn't require the CONTRACTS_FULL symbol I can keep it in my release builds as well. This is my consideration: Con: You can't call an overloaded version of the custom exception type