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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:33:59+00:00 2026-05-20T05:33:59+00:00

I might say I’m getting quite familiar with Code Contracts: I’ve read and understood

  • 0

I might say I’m getting quite familiar with Code Contracts: I’ve read and understood most of the user manual and have been using them for quite a while now, but I still have questions. When I search SO for ‘code contracts unproven’ there are quite a few hits, all asking why their specific statement couldn’t be statically proven. Although I could do the same and post my specific scenario (which is btw:

enter image description here),

I’d much rather understand why any Code Contract condition can or can’t be proven. Sometimes I’m impressed with what it can prove, and sometimes I’m… well… to say it politely: definately not impressed. If I want to understand this, I’d like to know the mechanisms the static checker uses. I’m sure I’ll learn by experience, but I’m spraying Contract.Assume statements all over the place to make the warnings go away, and I feel like that’s not what Code Contracts are meant for. Googling didn’t help me, so I want to ask you guys for your experiences: what (unobvious) patterns have you seen? And what made you see the light?

  • 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-20T05:33:59+00:00Added an answer on May 20, 2026 at 5:33 am

    The contract in your construction is not satisfied. Since you are referencing an object’s field (this.data), other threads may have access to the field and may change its value between the Assume and the first parameter resolution and the third parameter resolution. (e.i., they could be three completely different arrays.)

    You should assign the array to a local variable, then use that variable throughout the method. Then the analyzer will know that the constraints are being satisfied, because no other threads will have the ability to change the reference.

    var localData = this.data;
    if (localData == null) return;
    byte[] newData = new byte[localData.Length]; // Or whatever the datatype is.
    Array.Copy(localData, newData, localData.Length); // Now, this cannot fail.
    

    This has the added benifit of not only satisfying the constraint, but, in reality, making the code more robust in many cases.

    I hope this leads you to the answer to your question. I could not actually answer your question directly, because I do not have access to a version of Visual Studio that includes the static checker. (I’m on VS2008 Pro.) My answer is based on what my own visual inspection of the code would conclude, and it appears that the static contract checker uses similar techniques. I am intreagued! I need to get me one of them. 😀

    UPDATE: (Lots of speculation to follow)

    Upon reflection, I think I can do a pretty good guess of what can or can’t be proven (even without access to the static checker). As stated in the other answer, the static checker does not do interprocedural analysis. Therefore, with the looming possibility of multi-threaded variable accesses (as in the OP), the static checker can only deal effectively with local variables (as defined below).

    By “local variables” I mean a variable that cannot be accessed by any other thread. This would include any variables declared in the method or passed as a parameter, unless the parameter is decorated with ref or out or the variable is captured in an anonymous method.

    If a local variable is a value-type, then its fields are also local variables (and so on recursively).

    If a local variable is a reference-type, then only the reference itself—not its fields—can be considered a local variable. This is true even of an object constructed within the method, since a constructor itself may leak a reference to the constructed object (say to a static collection for caching, for example).

    So long as the static checker does not do any interprocedural analysis, any assumptions made about variables that are not local as defined above can be invalidated at any time, and, therefore, are ignored in the static analysis.

    Exception 1: since strings and arrays are known by the runtime to be immutable, their properties (such as Length) are subject to analysis, so long as the string or array variable itself is local. This does not include the contents of an array which are mutable by other threads.

    Exception 2: The array constructor may be known by the runtime not to leak any references to the constructed array. Therefore, an array that is constructed within the method body and not leaked outside of the method (passed as a parameter to another method, assigned to a non-local variable, etc.) has elements that may also be considered local variables.

    These restrictions seem rather onerous, and I can imagine several ways this could be improved, but I don’t know what has been done. Here are some other things that could, in theory, be done with the static checker. Someone who has it handy should check to see what has been done and what hasn’t:

    • It could determine if a constructor does not leak any references to the object or its fields and consider the fields of any object so constructed to be local variables.
    • A no-leaks analysis could be done on other methods to determine whether a reference type passed to a method can still be considered local after that method invocation.
    • Variables decorated with ThreadStatic or ThreadLocal may be considered local variables.
    • Options could be given to ignore the possibility of using reflection to modify values. This would allow private readonly fields on reference types or static private readonly fields to be considered immutable. Also, when this option is enabled, a private or internal variable X that is only ever accessed inside a lock(X){ /**/ } construction and which is not leaked could be considered a local variable. However, these things would, in effect, reduce the reliability of the static checker, so that’s kinda iffy.

    Another possibility that could open up a lot of new analysis would be declaratively assigning variables and the methods that use them (and so on recursively) to a particular unique thread. This would be a major addition to the language, but it might be worth it.

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

Sidebar

Related Questions

Might be a bit of noob question but it's something that's been getting me
Wow, what a stupid question you might say. But is it possible? I have
I'm quite stuck I might say dispite all other posts found on the site.
Typically I use E_ALL to see anything that PHP might say about my code
Let's say I have two models, Classes and People. A Class might have one
In object-oriented programming, we might say the core concepts are: encapsulation inheritance, polymorphism What
When say 3 programs (executables) are loaded into memory the layout might look something
So, the question might seem strange, but let's say that I compile: int main(void)
might be a silly question nonetheless: I'm playing around with the following code: $a='a';
I might have a flawed understanding of what shared_examples_for should do, but hear me

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.