Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6908327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:32:56+00:00 2026-05-27T08:32:56+00:00

I’m trying to understand Code Contracts in a bit more detail. I’ve got the

  • 0

I’m trying to understand Code Contracts in a bit more detail. I’ve got the following contrived example, where I’m trying to assert the invariant of a try/get pattern that if it returns true then the out object is non-null, otherwise if it returns false.

    public static bool TryParseFruit(string maybeFruit, out Fruit fruit)
    {
        Contract.Requires(maybeFruit != null);

        Contract.Ensures((Contract.Result<bool>() && Contract.ValueAtReturn(out fruit) != null) ||
                         (!Contract.Result<bool>() && Contract.ValueAtReturn(out fruit) == null));

        // Elided for brevity
        if (ICanParseIt())
        {
            fruit = SomeNonNullValue;
            return true;
        }
        else
        {
            fruit = null;
            return false;
        }
    }

I don’t like the duplication inside Contract.Ensures so I wanted to factor out my own method for this.

[Pure]
public static bool Implies(bool a, bool b)
{
   return (a && b) || (!a && !b);
}

Then I changed my invariant in TryParseFruit to

Contract.Ensures(Implies(Contract.Result<bool>(), Contract.ValueAtReturn(out fruit) != null);

But this generates warnings that the “ensures is unproven”. If I then perform the inline refactoring on my Implies method then everything is OK again.

Could someone explain to me why this happens? I’m guessing it’s because Contract.ValueAtReturn is used magically in some way that means I can’t just pass it’s result off to another function and expect it to work.

(Update #1)

I think that all of the following Contract.Ensures express the same thing (namely that if the function returns true then fruit is non-null, otherwise fruit is null). Note that I am only using one of these at a time 🙂

Contract.Ensures(Implies(Contract.Result<bool>(), Contract.ValueAtReturn(out fruit) != null));           
Contract.Ensures(Contract.Result<bool>() == (Contract.ValueAtReturn(out fruit) != null));
Contract.Ensures(Contract.Result<bool>() ^ (Contract.ValueAtReturn(out fruit) == null));

However, none of the above Contract.Ensures lead to a clean compile of the code below. I want the Code.Contracts to express that fruit.Name cannot be a null reference.

    Fruit fruit;
    while (!TryGetExample.TryParseFruit(ReadLine(), out fruit))
    {
        Console.WriteLine("Try again...");
    }

    Console.WriteLine(fruit.Name);

I am only able to get the completely clean compile with code contracts if I use the long winded way of expressing this detailed above. My question is why!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:32:57+00:00Added an answer on May 27, 2026 at 8:32 am

    Of course, you can also try Contract.Ensures(Contract.Result<bool>() == (Contract.ValueAtReturn(out fruit) != null)); I have a vague recollection that the analyzer prefers == to other operators.

    I’ve had some success tracing these things with Contract.Assert which helps you find out where the hole in the analysis lies. I have also found cases where Contract.Assert allows the analysis to succeed. In other words, you might fix this with a couple of assertions:

    public static bool TryParseFruit(string maybeFruit, out Fruit fruit) 
    { 
        Contract.Requires(maybeFruit != null); 
    
        Contract.Ensures(Implies(Contract.Result<bool>(), Contract.ValueAtReturn(out fruit) != null);
    
        // Elided for brevity 
        if (ICanParseIt()) 
        { 
            fruit = SomeNonNullValue; 
            Contract.Assert(Implies(Contract.Result<bool>(), Contract.ValueAtReturn(out fruit) != null);
            return true; 
        } 
        else 
        { 
            fruit = null; 
            Contract.Assert(Implies(Contract.Result<bool>(), Contract.ValueAtReturn(out fruit) != null);
            return false; 
        } 
    } 
    

    Messy, I know. On the other hand, if any assertion fails, you can look, for example, at other aspects of the logic. For example, you can litter your code with Contract.Assert(SomeNonNullValue != null); to see where the analyzer is losing its certainty about the non-nullness of SomeNonNullValue.

    EDIT

    If the Assert is unproven, but you know that it should be provable, then you can use that to help isolate the problem. I suspect that the problem (or at least part of it) is your lack of Ensures in the Implies method. Try adding Contract.Ensures(Contract.Result<bool>() == (Contract.OldValue(a) == Contract.OldValue(b))); Also, as I again have vague recollections about different handling for different logical operators, try restating the return of that method. For example: return a == b;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
i got an object with contents of html markup in it, for example: string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.