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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:20:44+00:00 2026-06-04T09:20:44+00:00

Suppose you have an existing large project and you want to integrate Code Contracts

  • 0

Suppose you have an existing large project and you want to integrate Code Contracts in it. The existing code uses if-null-then-throw logic. For the given conditions, the documentation suggests to set the assembly mode to Custom Argument Validation.

I have the following classes:

class A
{
    protected virtual void Foo(int a, int b)
    {
        if (a == null)
            throw new ArgumentNullException(a);
        if (b == null)
            throw new ArgumentNullException(b);
        Contract.EndContractBlock();
    }
}
class B : A
{
    protected override void Foo (int a, int b)
    {
        // some stuff
        base.Foo(a, b);
    }
}

When I compile I get the following warning:

warning CC1055: Method ‘B.Foo(int, int)’ should contain custom
argument validation for ‘Requires(a != null)’
as it overrides ‘A.Foo(int,int)’ which suggests it does. If you don’t
want to use custom argument validation in this assembly, change the
assembly mode to ‘Standard Contract Requires’.

I don’t want to repeat the preconditions on every overridden method! Is there a way around it?

  • 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-04T09:20:45+00:00Added an answer on June 4, 2026 at 9:20 am

    It works fine if you use Contract.Requires() instead of Contract.EndContractBlock().

    There is a section in the manual quoted below which suggests adding a [SuppressMessage] attribute to the method override.

    From the Code Contracts user manual p.22 section 5.2.3.

    Delegating Checks to Other Methods

    Suppose you have a code pattern similar to the following code:

    public class Base {
        public virtual void Compute(string data) {
        if (data == null) throw new ArgumentNullException(...);
            Contract.EndContractBlock();
            ...
        }
    }
    
    public class Derived : Base {
        public override void Compute(string data) {
            base.Compute(data);
            ...
        }
    }
    

    Then the tools will issue warning CC1055 with a message of the form:

    Method ’Derived.Compute’ should contain custom argument validation for
    ‘ Requires (ArgumentNullException)(data ! = null)’ as it overrides
    ‘Base.Compute’ which suggests it does.

    In this situation, the warning is not helpful, as the implementation
    of Derived.Compute delegates the parameter validation to another
    method (in this case the base method). To avoid the warning in this
    situation without repeating the validation, you can add a
    SuppressMessage attribute to the method:

    public class Derived : Base {
        [SuppressMessage("Microsoft.Contracts", "CC1055", Justification = "Validation performed in base method")]
        public override void Compute(string data) {
            base.Compute(data);
            ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So suppose I have two Models -- Project and Task I want user to
Suppose I have an IEnumerable such as a List(TValue) and I want to keep
Suppose I have a PL/SQL stored procedure as follows: PROCEDURE do_something(foo VARCHAR2 DEFAULT NULL)
Suppose I have a Dictionary<String,String> , and I want to produce a string representation
I have an existing Mediawiki installation, for which I suppose I chose the MySQL
Suppose I have a hex number 4072508200000000 and I want the floating point number
I'm investigating on how validates_presence_of actually works. Suppose I have two models class Project
We have a (very large) existing codebase for a custom ActiveX control, and I'd
Suppose i have an existing site with a good user base and active content.
Suppose I have an existing assembly, and some of the classes have overloaded methods,

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.