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 8284973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:18:20+00:00 2026-06-08T11:18:20+00:00

I think I found a bug in the static contract checking tool. Is there

  • 0

I think I found a bug in the static contract checking tool. Is there a way besides tagging the whole thing with [ContractVerification(false)] ?

class Employee
{
    public int? HierarchyLevel { get; private set; }

    public Employee(int? level)
    {
        Contract.Requires<ArgumentException>(
            (!level.HasValue) 
            || 
            level >= 0 && level <= 10);
        Contract.Ensures(
            (!HierarchyLevel.HasValue) 
            || 
            (HierarchyLevel.Value >= 0 && HierarchyLevel.Value <= 10));
        this.HierarchyLevel = level; //unproven

        // this doesnt work either
        //if (!level.HasValue) {
        //    this.HierarchyLevel = new Nullable<int>();
        //} else {
        //    this.HierarchyLevel = new Nullable<int>(level.Value);
        //}

        // can't even make the static checker shut up with that:
        //Contract.Assume(
        //    (!HierarchyLevel.HasValue) 
        //    || 
        //    (HierarchyLevel.Value >= 0 && HierarchyLevel.Value <= 10));
    }
}

Surprisingly, The version below works. But I don’t want to start writing unidiomatic code or introduce my own version of Nullable just to please the contract verification tool.

class Employee2
{
    public int HierarchyLevel { get; private set; }
    public bool HasLevel { get; private set; }
    public Employee2(int level, bool hasLevel)
    {
        Contract.Requires<ArgumentException>(!hasLevel || level >= 0 && level <=10);
        if (hasLevel) {
            HasLevel = true;
            HierarchyLevel = level;
        } else {
            HasLevel = false;
            HierarchyLevel = -1;
        }
    }
    [ContractInvariantMethod]
    private void ObjectInvariant()
    {
        Contract.Invariant(
            (!HasLevel) || 
            (HierarchyLevel >= 0 && HierarchyLevel <= 10));
    }
}
  • 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-06-08T11:18:21+00:00Added an answer on June 8, 2026 at 11:18 am

    First of all, you have misplaced some braces in your code (but this does not solve your problem):

    Contract.Requires<ArgumentException>(!level.HasValue
        || (level.Value >= 0 && level.Value <= 10));
    Contract.Ensures(!HierarchyLevel.HasValue
        || (HierarchyLevel.Value >= 0 && HierarchyLevel.Value <= 10));
    

    I think you may be right: the static checker cannot prove everything yet, and also depends on the Base Class Library. People in this thread seem to be talking about how the static checker cannot properly understand nullable types due to an error in the contract implementation of nullable types in general.

    Of course, you can also write the following, which solves your problem:

    Contract.Requires<ArgumentException>(!level.HasValue
        || (level.Value >= 0 && level.Value <= 10);
    Contract.Ensures(HierarchyLevel == level);
    

    And here is another solution to your problem. Put the conditions into a separate method, and mark the method with the PureAttribute (indicating that the method has no side-effects). Then, apply the method on both the incoming argument and the ensured value, like this:

    [Pure]
    public static bool IsInRange(int? value)
    {
        return !value.HasValue
            || (value >= 0 && value <= 10);
    }
    
    public Employee(int? level)
    {
        Contract.Requires<ArgumentException>(IsInRange(level));
        Contract.Ensures(IsInRange(HierarchyLevel));
        this.HierarchyLevel = level;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I found a bug in opencart so in the featured module there
What if I think, that I found a bug in an open-source-app? What steps
I've found what I think may be a bug with Ivar and Objective-C runtime.
While searching a bug in my code today I found a strange thing. When
I found a way to write the if statement in another way (I think)
I think I have found a bug when setting or getting the Me.Top property
I think I may have found a bug in PHP's crypt() function under Windows.
I found an interesting bug and wanted to know you think. Brief background: I've
I think I've found a bug in IE's (IE8) handling for the for-in javascript
I think I've found a bug in safari. I wondered if anyone has encountered

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.