code-contracts

Books on Code Contracts in C# 4.0 [closed]

一笑奈何 提交于 2019-12-01 06:30:29
Altough I've known Code Contracts for some time, as I've used it a bit in Java, I would like to start using them in C#, now that they are part of C# 4.0. I am looking on learning material, books or vids. Altough tutorials are also welcome, I'd like to have something comprehensive to read, not the typical "here's how you use Requires and here's how you use Ensures". I am looking for something that really stands out in this area. Thanks Jon Skeet has an entire chapter dedicated to them in the 2nd edition of C# in Depth . It's roughly 40 pages based on the last MEAP update. This isn't final since

How to avoid “source !=null” when using Code Contracts and Linq To Sql?

▼魔方 西西 提交于 2019-12-01 03:29:21
问题 I have the following code using a normal data context which works great: var dc = new myDataContext(); Contract.Assume(dc.Cars!= null); var cars = (from c in dc.Cars where c.Owner == 'Jim' select c).ToList(); However when I convert the filter to an extension method like this: var dc = new myDataContext(); Contract.Assume(dc.Cars!= null); var cars = dc.Cars.WithOwner('Jim'); public static IQueryable<Car> WithOwner(this IQueryable<Car> cars, string owner) { Contract.Requires(cars != null);

Can I leave contracts in code that I'm merging with a codebase used by non-code contracts developers?

梦想的初衷 提交于 2019-12-01 02:50:10
For the last few months I've been developing a side project for my company, but the higher-ups have now decided it would be a good fit in an existing product. I've been developing the side project using Microsoft's Code Contracts for static type checking (partly because I hadn't used them before and was eager to learn). My problem is that if I check in my code to the code base with Contracts in place, will every other developer need the Code Contracts tools installed to be able to continue developing? I know for a fact that none of them have it installed, and I'm the junior here so I doubt I

Code Contract : ccrewrite exited with code -1?

微笑、不失礼 提交于 2019-11-30 18:08:42
I'm new to code contracts. I downloaded the latest build of code contract project (1.4.40314.1) and started to implement it in my project. When i enabled 'Runtume Checking' through Code Contracts Tab in VS2010, i got this Error Error 1 The command ""C:\Program Files (x86)\Microsoft\Contracts\Bin\ccrewrite" "@Application1ccrewrite.rsp"" exited with code -1. everytime i build the project. Plz help. Now it's a major problem for me. Every project using code contracts is showing same error in VS2010 Errors window and 'Application1ccrewrite.rsp' not found in output window, but it is there. I tried

How Do You Configure Pex to Respect Code Contracts?

試著忘記壹切 提交于 2019-11-30 15:15:35
问题 Given the following example code, how can I configure Pex to respect my Code Contracts? public static IEnumerable<User> Administrators(this UserGroup userGroup) { Contract.Requires(userGroup != null); Contract.Requires(userGroup.UserList != null); return userGroup.UserList.Where(ul => ul.IsAdmin == true); } Current Problem: When I run Pex, it's still generating test cases which violate the specified code contracts. FYI: Here are the 'Code Contracts' settings in my csproj file. EDIT: Did

Building with Code Contracts?

自作多情 提交于 2019-11-30 08:26:44
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 the exception, it shows this: Description: An assembly (probably "CodeContractsTest") must be rewritten

Design by contracts and constructors

主宰稳场 提交于 2019-11-30 08:19:44
问题 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

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

痞子三分冷 提交于 2019-11-30 05:38:25
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 accepted answer: public double CalculateTotal(Order order) { Contract.Requires(order != null); Contract

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

烂漫一生 提交于 2019-11-30 04:54:58
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 Code Contracts were introduced in .NET 4.0 and they provide a language-agnostic method to express coding assumptions in programs. They basically allow you to check for pre-conditions, post-conditions and other features and can greatly improve the testing process and the eventual quality of

Code Contract : ccrewrite exited with code -1?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 02:02:06
问题 I'm new to code contracts. I downloaded the latest build of code contract project (1.4.40314.1) and started to implement it in my project. When i enabled 'Runtume Checking' through Code Contracts Tab in VS2010, i got this Error Error 1 The command ""C:\Program Files (x86)\Microsoft\Contracts\Bin\ccrewrite" "@Application1ccrewrite.rsp"" exited with code -1. everytime i build the project. Plz help. Now it's a major problem for me. Every project using code contracts is showing same error in