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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:36:37+00:00 2026-06-11T05:36:37+00:00

As soon as you disable runtime contract checking in a project’s properties, FxCop violations

  • 0

As soon as you disable runtime contract checking in a project’s properties, FxCop violations are thrown for the contract classes (assuming you implement contracts via an interface, then define the contract in an abstract class).

Re-enable runtime contract checking and all the violations disappear..

What is the reason for this?

Violations like:

CA1811 ObjectInvariant appears to have no upstream callers

and

CA1033 Make MyClassContract sealed

which don’t make sense since contract classes must be abstract.

  • 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-11T05:36:39+00:00Added an answer on June 11, 2026 at 5:36 am

    Okay, I think I see what you mean. You have class like Kevin’s AccountContracts named MyClassContract with a private method named ObjectInvariants instead of Invariants:

    [ContractClassFor(typeof(IAccount))]
    public abstract class MyClassContract : IAccount
    {
        public abstract double Balance { get; }
    
        void IAccount.Deposit(double amount)
        {
            Contract.Requires(amount >= 0.0d);
    
            //throw new NotImplementedException();
        }
    
        bool IAccount.Withdraw(double amount)
        {
            Contract.Requires(amount >= 0.0d);
            Contract.Requires(amount <= Balance);
    
            throw new NotImplementedException();
        }
    
        [ContractInvariantMethod]
        private void ObjectInvariants()
        {
            Contract.Invariant(Balance >= 0.0d);
        }
    }
    

    FxCopy and Code Analysis does post-compile analysis. It analyses the binary, in other words the Intermediate Language (IL) that got generated by the build. Code Contracts kind of does a post-compile weave in that it generates code into the binary that not only derives from your abstract class MyClassContract as well as invokes the ObjectInvariants method at least once.
    When you “turn off” Code Contracts, it no longer generates this code so FxCop and Code Analysis see nothing that derives from MyClassContract nor anything that uses ObjectInvariants method and thus warns you. In the case of MyClassContract, since nothing derives from it, it can be made sealed (which helps the compiler optimize slightly under certain circumstances) as well as makes the class easier to maintain in the future (nothing can derive from it, so you’re more free to change it–at least that’s the general consensus).

    You can, of course, suppress those warnings in a suppression file, if you want the code to remain as is without the warnings. You could also include a compiler constant and surround the code to avoid it compling when not using code contracts and include that constant in the build settings when you enable code-contracts. For example:

    #if CODE_CONTRACTS
    //...
    #endif
    

    Creating a new build-config is usually the easiest thing for this because you can have a particular build configuration that both enables code contracts and declares CODE_CONTRACTS in the projects Build properties.

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

Sidebar

Related Questions

Soon enough I will be forced to present a project in Haskell that is
I will soon be starting a final year Engineering project, consisting of the real-time
I'm working on a project that will (soon) be branched into multiple different versions
I'm not currently working on a coding project that is trying to implement this,
I have a asp.net app that I want to disable the buttons as soon
Soon I'll be launching my new site and i was planning on using gmail
As soon as the user clicks the delete button my jQuery script asks the
How soon after the reference count reaches zero is __del__ method called? Does the
As soon as View Loads I want to display a paused video, with its
In my application as soon as an object is dropped on canvas I stored

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.