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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:54:37+00:00 2026-05-28T05:54:37+00:00

Basically I’m looking at 2 different situations: Method calls within the same class: public

  • 0

Basically I’m looking at 2 different situations:

Method calls within the same class:

public class MyClass
{
    public Bar GetDefaultBar(Foo foo)
    {
        Contract.Requires(foo != null);

        return GetSpecificBar(foo, String.Empty);
    }

    public Bar GetSpecificBar(Foo foo, string name)
    {
        Contract.Requires(foo != null);
        Contract.Requires(name != null);

        ...
    }
}

Method calls within different classes:

public class MyClass
{
    private MyBarProvider { get; set; }

    public Bar GetDefaultBar(Foo foo)
    {
        Contract.Requires(foo != null);

        return BarProvider.GetSpecificBar(foo, String.Empty);
    }

    //Object-Invariant ensures that MyBarProvider is never null...
}

public class MyBarProvider
{
    public Bar GetSpecificBar(Foo foo, string name)
    {
        Contract.Requires(foo != null);
        Contract.Requires(name != null);

        ...
    }
}

I’m wondering if it is necessary to have duplicate contracts for either of these situations? I’m guessing that there might be a way to avoid it in the first example (all within the same class), but not in the second example (different classes). Also, should I avoid duplicating or should it be in there?

  • 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-28T05:54:38+00:00Added an answer on May 28, 2026 at 5:54 am

    I assume that the duplication you’re talking about is Contract.Requires(foo != null);.

    You need to have that line in both methods, in both cases. If you don’t include it in the caller, you’ll get a “Requires unproven” at the call site, because the calling method’s caller might pass a null value. If you don’t include it in the callee, the callee’s contract won’t include the non-null requirement, and the contract analyzer will consider foo to be possibly null. That’s because another call site would be free to pass in a null value.

    EDIT

    As Ɖiamond ǤeezeƦ points out, you can use the ContractAbbreviatorAttribute to refactor repetitive contract calls into a separate method. The attribute is not part of .NET Framework 4.0, but you can define it yourself. For more information, see Terje Sandstrom’s blog post on reconciling code contracts null checks with FXCop: http://geekswithblogs.net/terje/archive/2010/10/14.aspx

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

Sidebar

Related Questions

Basically, I have the following code: public class MyDictionary<TKey, TValue> : IDictionary<TKey, TValue> {
Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer
Basically I have 2 strings. They are EXACTLY the same string. However, when I
Basically what I am after is this, var foo = 'I am foo!'; alert(window['foo']);
Basically, I want to modify the constructor of the Object class. Since every class
Basically, I'm just looking for pointers here. Ideally, I'd just supply a GPS coordinate
Basically, I've seen this used all to often: public event MyEventHandler MyEvent; private void
Basically I have an entity (called Session) which will provide many different Services. Each
Basically, I have something like the following: public string SomeDBMethod(string server, string dbName, string
Basically what I want to do it this: a pdb file contains a location

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.