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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:46:11+00:00 2026-05-28T03:46:11+00:00

I might be misunderstanding code contracts, but here’s my situation. I have the following

  • 0

I might be misunderstanding code contracts, but here’s my situation.

I have the following code:

interface IFetch<T>    // defined in another DLL
{
    T Fetch(int id);
}

interface IUserFetch : IFetch<User>
{
    IEnumerable<User> GetUsersLoggedIn ();
}

class UserFetch : IUserFetch
{
    public User Fetch(int id)
    {
        return (User) Database.DoStuff (id);
    }

    public IEnumerable<User> GetUsersLoggedIn ()
    {
        return (IEnumerable<User>) Database.DoMoreStuff ();
    }
}

I’m trying to add a relatively simple contract: Contract.Requires (id != 0);, and I want it validated on Fetch. When I add it to Fetch directly, I get the warning that Method Fetch(int id) implements interface 3rdParty.IFetch<User> and thus cannot add Requires.

I created an abstract code contracts class implementing IFetch and pointed it to/from UserFetch using the ContractClass and ContractClassFor attributes respectively. I still get an error like CodeContracts: The class 'FetchUserContracts' is supposed to be a contract class for '3rdParty.IFetch<User>', but that type does not point back to this class. However, since 3rdParty.IFetch is a generic type, I don’t think I can ever put a code contract on it specifically.

Has the problem been made clear, and if so, how do I solve 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-05-28T03:46:12+00:00Added an answer on May 28, 2026 at 3:46 am

    I believe Ɖiamond ǤeezeƦ’s answer is correct. I will just add a bit of explaination.

    You cannot add a Contract.Requires decoration to a method in a closed constructed type (such as IFetch<User>). You must add it to the open constructed type (IFetch<>). The reason for this is exactly the same reason that you cannot add a Contract.Requires decoration to a concrete method that is used to implements an interface: code contracts are designed to be verifiable at compile time when the full type of an object instance may be unknown.

    Suppose it did allowed you to put the contract on the concrete method implementation.

    public User Fetch(int id) 
    { 
        Contract.Requires (id != 0);, 
        return (User) Database.DoStuff (id); 
    } 
    

    Now suppose someone tried to use a variable of the interface type.

    class DataDisplayer<T>
    {
        Label myLabel = new Label();
        public void Display(IFetch<T> fetch, int id)
        {
            myLabel.Text = fetch.Fetch(id).ToString();
        }
    }
    

    Does this allow a contract violation or not? It is impossible to tell, because we don’t know what the concrete type of fetch will be at runtime.

    Putting the contract on a particular closed constructed type like IFetch<User> does not solve this problem. We still don’t know what type T is in the calling code.

    The Liskov Substitution Principle, which is a cornerstone of all object orient programming, means that we can never assume anything about the runtime type of an object beyond what the variable’s type indicates. And since the variable’s type may be a generic interface, code contracts cannot place any additional burden on the caller than what is in the generic interface definition.

    Therefore, Contract.Requires must be put on the open constructed type, so that any object that implements the interface will have the requirement, and any potential call to the method through a variable of the interface type can be verified for correctness with respect to that requirement.

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

Sidebar

Related Questions

Might be subjective and/or discussion.. but here goes. I've been asked to estimate a
This might be a fundamental jQuery misunderstanding but I've got to ask... I'm trying
I may have a misunderstanding but I was under the impression that having a
I might be mis-understanding here but from my understanding a markdown editor strips out
This might seem like a stupid question I admit. But I'm in a small
This might be on the discussy side, but I would really like to hear
This might be a bit on the silly side of things but I need
You might have a set of properties that is used on the developer machine,
This might seem obvious but I've had this error when trying to use LINQ
This might sound like a little bit of a crazy question, but how can

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.