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

  • Home
  • SEARCH
  • 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 3943700
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:48:46+00:00 2026-05-20T00:48:46+00:00

I have a situation I don’t know how it’s supposed to be solved. According

  • 0

I have a situation I don’t know how it’s supposed to be solved.
According to the user manual section 3, a contractmethod, i.e. Require or Ensure, is not allowed in overriding methods/properties or interface implementations. The contract methods should be declared in the root virtual/abstract method and since you can’t declare code in an abstract method, you have to work with the ContractClassAttribute and ContractClassForAttribute. The same goes for contractmethods for interfacemembers(and their implementations).

But what if I want to use an interface I haven’t created myself? For example IList<T> doesn’t implement these contract methods, but I can’t set the ContractClassAttribute on it either. How should I do parametervalidation in for example the implementation of IList<T>.this[int]? The following solutions aren’t allowed:

T IList<T>.this[int i]
{
    get
    {
        Contract.Requires(i >= 0);//not allowed

        if (i < 0)
            throw new ArgumentException();
        Contract.EndContractBlock();//also not allowed
        ...
    }
}

Are the legacy if-then-throw statements without EndContractBlock the only solution?

  • 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-20T00:48:47+00:00Added an answer on May 20, 2026 at 12:48 am

    First of all, IList<T> already specifies that precondition. I just tried it to be sure: “Precondition failed: index >= 0”.

    Furthermore, as I understand it, post conditions are allowed, only preconditions aren’t.

    If, you would still like to add other preconditions, I believe this answers your question:
    http://social.msdn.microsoft.com/Forums/en/codecontracts/thread/af403bbc-ca4e-4546-8b7a-3fb3dba4bb4a

    It basicly comes down to the fact that you are not meant to do so, as that would defy the purpose of code contracts.

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

Sidebar

Related Questions

I don't know what it's doing when we have this situation: Foo::Bar It looks
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have a situation where I definitely don't want to get the whole domain
I have a situation in which I don't want inserts to take place (the
I have a situation where I have an xml file that I don't want
I have very simple situation and really don't have a clue why this isn't
I'm learning to use Grails and have run into a situation I don't understand
I have a situation where I need to have a user select a region
I have forgotten my Keystore password and I don't really know what to do
We have a situation where users are allowed to upload content, and then separately

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.