code-contracts

CodeContracts: ccrewrite fails with Object reference not set to an instance of an object

The below code makes ccrewrite blow up! Ideas? BTW, If you comment out the ActualClass, ccrewrite succeeds... [ContractClass(typeof(TestContracts))] interface ITestInterface { bool IsStarted { get; set; } void Begin(); } class ActualClass : ITestInterface { public bool IsStarted { get; s...

What is a practical usage of Code Contracts in .NET 4.0?

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 the Code Contracts. Anyone has a good example of application of this feature? I would like to get a code sample with a brief explanati...

Can one make Code Analysis understand Code Contracts?

Hi all, When using Code Analysis and Code Contracts in combination, I get a lot of warnings like warning : CA1062 : Microsoft.Design : In externally visible method 'Foo.Bar(Log)', validate parameter 'log' before using it. In Foo.Bar, I have a Contract that validates 'log'. public Bar(Log log){ Contract.Requires(log != null); lo...

In C# should I use uint or int for values that are never supposed to be negative?

Possible Duplicate: Should I use uint in C# for values that cant be negative? Suppose that the MaxValue of (roughly :) ) 2^31 vs 2^32 does not matter. On one hand, using uint seems nice because it is self-explanatory, it indicates (and promises?) that some value may never be negative. However, int is more common, and a cast is...

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

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(); Cont...

Code Contracts Static Analysis: Prover Limitations?

I've been playing with Code Contracts and I really like what I've seen so far. They encourage me to evaluate and explicitly declare my assumptions, which has already helped me to identify a few corner cases I hadn't considered in the code to which I'm adding contracts. Right now I'm playing with trying to enforce more sophisticated inv...

How to avoid Linq chaining to return null?

I have a problem with code contracts and linq. I managed to narrow the issue to the following code sample. And now I am stuck. public void SomeMethod() { var list = new List<Question>(); if (list.Take(5) == null) { } // resharper hints that condition can never be true if (list.ForPerson(12) == null) { } // resharpe...

Really trying to like CodeContracts in C#

First post, so please be kind... I am finally playing catchup with everything new that has been added in to the .NET 3.5/4.0 Frameworks. The last few days I have been working with CodeContracts and I am really trying hard to like them. I am curious what other people think of the implementation of CodeContracts in C#? Specifically, how a...

Why won't this Code Contracts relationship prove?

I have a method that starts like this: public static UnboundTag ResolveTag(Type bindingType, string name, string address) { Contract.Requires(bindingType != null); var tags = GetUnboundTagsRecursively(bindingType).ToArray(); The contract for the implementation of GetUnboundTagsRecursively (implemented in the s...

Using Contract.ForAll in Code Contracts

Okay, I have yet another Code Contracts question. I have a contract on an interface method that looks like this (other methods omitted for clarity): [ContractClassFor(typeof(IUnboundTagGroup))] public abstract class ContractForIUnboundTagGroup : IUnboundTagGroup { public IUnboundTagGroup[] GetAllGroups() { Contract.Ensu...

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

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 Contra...

.NET 4 Code Contracts: do I need to include the same contracts twice?

Let's say I have a method public void Foo(string bar) that the caller should not call with a null value of bar. Let's say I also have a method, call it private void FooImpl(string bar), that does the actual work of Foo. It's, of course, FooImpl that really requires non-nullness of bar, even though Foo is the public interface. And lets sa...

Code contracts usage patterns when dealing with Files

I ve just started using Code contract with .net and I had a Guard clause like this if (!file.Exists(path)) throw FileNotFoundException(); and replaced it with Contract.Requires(File.Exists(path)); Note: the original question had a typo and it used to read Contract.Ensures() I m not sure this is correct, because the contract wil...

Code contracts for .NET 3.5 messes up VS10's debugger

I've recently migrated a lot of manual precondition testing and exception throwing with code contracts. Instead of upgrading to .NET 4, I've been using the Microsoft.Contracts.dll assembly so I could stick to .NET 3.5 a bit longer (this is a library that is used both by .NET 3.5 and .NET 4 assemblies). I've set up the contracts rewriter ...

Code Contracts: How to deal with inherited interfaces?

I'm using MS Code Contracts and have run into a snag with using interface inheritance and ContractClassFor attributes. Given these interfaces and contract classes: [ContractClass(typeof(IOneContract))] interface IOne { } [ContractClass(typeof(ITwoContract))] interface ITwo : IOne { } [ContractClassFor(typeof(IOne))] abstract class IOn...

Is there any reason not to use Run-time Contract Checking with Code Contracts?

I've recently listened to Kevin Hazzard talk about Code Contracts in .Net Rocks show 570 (http://devjourney.com/community/dotnet-rocks-show-570-with-kevin-hazzard/). He mentions enabling Run-time Contract Checking as an option some people might choose to use while others might not. Why would you not use Run-time Contract Checking for yo...

Does code contracts really help unit testing?

I have fair amount of knowledge on unit testing. I have been trying to read about code contracts. Does it really help unit testing? Is it over-rated especially when we talk about code-contract helping to do unit testing. I am specifically referring to contracts in .net 4.0. I use nunit for unit testing. ...

Code Contracts: Why are some invariants not considered outside the class?

Consider this immutable type: public class Settings { public string Path { get; private set; } [ContractInvariantMethod] private void ObjectInvariants() { Contract.Invariant(Path != null); } public Settings(string path) { Contract.Requires(path != null); Path = path; } } Two th...

Validation in ASP.NET MVC using Code Contracts

I want to know the available options for using the "Code Contracts" attributes as the validations rules in ASP.NET MVC 2. ...

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

Hi there this is my scenario public interface ISomething { void DoStuff(); //... } public class Something : ISomething { private readonly ISomethingElse _somethingElse; //... public Something (ISomethingElse somethingElse) { Contract.Requires(somethingElse != null); _somethingElse = somethingE...