Questions tagged [microsoft-contracts]

22 questions
52
votes
7 answers

ReSharper - Possible Null Assignment when using Microsoft.Contracts

Is there any way to indicate to ReSharper that a null reference won't occur because of Design-by-Contract Requires checking? For example, the following code will raise the warning (Possible 'null' assignment to entity marked with 'NotNull'…
43
votes
4 answers

How mature is the Microsoft Code Contracts framework?

Microsoft has recently put a release of their Code Contracts framework on DevLabs with a commercial license. We're interested on using them in our project (mostly C#, some C++/CLI) to gradually replace all the custom validation code, but I'm keen to…
Greg Beech
  • 122,952
  • 42
  • 199
  • 241
37
votes
4 answers

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

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…
Finglas
  • 15,050
  • 9
  • 51
  • 85
32
votes
1 answer

C#: Code Contracts vs. normal parameter validation

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); …
steveee
  • 321
  • 3
  • 8
7
votes
2 answers

Can Microsoft Code Contracts be used with an ASP.NET Website?

I'm currently using Microsoft Code Contracts in an ASP.NET MVC application without any issues but I can not seem to get it quite running in a basic ASP.NET Web site. I'm not entirely sure it was made to work with this type of project (although it…
jamesmillerio
  • 3,016
  • 2
  • 22
  • 32
7
votes
5 answers

Bug in iterators with code contracts?

The following code fails on the pre condition. Is this a bug in code contracts? static class Program { static void Main() { foreach (var s in Test(3)) { Console.WriteLine(s); } } static…
pn.
  • 822
  • 1
  • 7
  • 9
5
votes
1 answer

.NET code contracts: can it get more basic than this?

I was just messing around to answer someone's question here on Stack Overflow, when I noticed a static verification warning from inside my Visual Studio (2008): string[] source = { "1", "A", "B" }; var sourceObjects = Array.ConvertAll(source, c =>…
Thorarin
  • 43,849
  • 11
  • 70
  • 108
4
votes
2 answers

What tooling do you use to do Design by Contract?

I used to use Microsoft CodeContracts for three weeks and now half of my code is just contracts. I have dozens of unproved places, I cannot use runtime-check because IL rewrite prevents coverage tool to show something and compile time is less then…
Mike Chaliy
  • 23,606
  • 16
  • 61
  • 102
4
votes
5 answers

Why am I getting malformed contract, in by C# code?

Visual Studio shows an error when I write this contract below. Error 20 Malformed contract section in method '....get_Page' Is the problem with the if block? public int? Page { get { int? result = Contract.Result(); if (result…
Victor Rodrigues
  • 10,673
  • 22
  • 73
  • 106
3
votes
1 answer

Code Contracts with new MVC 3 ViewBag

public class HomeController : Controller { public ActionResult Index() { // Warning 19 CodeContracts: Possibly calling a method on a null reference 'Website.Controllers.HomeController.o__SiteContainer0.<>p__Site2.Target'…
2
votes
2 answers

What does it take to prove this Contract.Requires?

I have an application that runs through the rounds in a tournament, and I am getting a contract warning on this simplified code structure: public static void LoadState(IList stuff) { for(int i = 0; i < stuff.Count; i++) …
John Gietzen
  • 45,925
  • 29
  • 140
  • 183
1
vote
1 answer

Could not load file or assembly 'Facebook.Web.Contracts' or one of its dependencies

Just downloaded the facebook c# sdk. Haven't written any code just added my references to the dll's and ran the project, as I already had some other code I was working on. Then I got this error above. I've definitely added that dll as well as the…
user583824
1
vote
2 answers

How do I indicate that a method never returns a null using code contracts?

How do I indicate that a method never returns a null? Currently this is my code. Line 19 gets an Ensures not proven message, even though CreateFunction assumes that the result is not nothing. 1 Public Function CreateFunction(Of…
Jonathan Allen
  • 63,625
  • 65
  • 234
  • 426
1
vote
2 answers

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?
x2.
  • 9,276
  • 5
  • 36
  • 62
1
vote
1 answer

Convert ImageSource into IRandomAccessStream

I have loaded image im my app with type ImageSource, and I want to share it, but I need to convert image to IRandomAccessStream. For e.g.: ImageSource _myLoadedImage; IRandomAccessStream ras; //Some code when _myLoadedImage convert to…
Viacheslav
  • 141
  • 1
  • 13
1
2