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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:51:10+00:00 2026-05-15T14:51:10+00:00

Let’s say I have a method public void Foo(string bar) that the caller should

  • 0

Let’s say I have a method public void Foo(string bar) that the caller should not call with a null value of bar. Let’s say I also have a method, call it private void FooImpl(string bar), that does the actual work of Foo. It’s, of course, FooImpl that really requires non-nullness of bar, even though Foo is the public interface. And lets say I want to enforce this non-nullness using the .NET 4.0 code contracts.

Where do I put the contract?

If I do this:

public void Foo(string bar)
{
  this.FooImpl(bar);
}

private void FooImpl(string bar);
{
  Contract.Requires<ArgumentNullException>(bar != null);

  // Something that requires non-nullness, e.g.:
  bar.Contains("test");
}

then the static checker complains that Foo is calling FooImpl with a possibly-null value, and suggests I add the non-null contract to Foo. OK, so I guess I can’t delegate my contract-checking/exception-throwing to the implementation methods.

But if I try to put it in the public interface, i.e.:

public void Foo(string bar)
{
  Contract.Requires<ArgumentNullException>(bar != null);

  this.FooImpl(bar);
}

private void FooImpl(string bar);
{
  bar.Contains("test");
}

then the static checker complains that FooImpl is calling Contains on a possibly-null value—even though the only place from which FooImpl is ever called in the code is from Foo, which itself ensures that it will never call FooImpl with a null value.


So, do I need to include the same contract twice? Or should I just ignore the static checker? I know it’s kind of a source of busywork and shouldn’t be relied upon, but I would hope that it had some way of handling this basic, and presumably common, scenario.

  • 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-15T14:51:11+00:00Added an answer on May 15, 2026 at 2:51 pm

    Short answer: Yes.

    You should add preconditions everywhere you want Code Contracts to prevent things like nullreference exceptions. This sometimes means it will look like you are adding the same contracts twice.

    In this particular case, it’s obvious to you that FooImpl is only called from a method which already has a precondition.

    However, the static checker evaluates the FooImpl method independently from other methods.
    In this example you just pass the bar value from Foo ( where you know bar is not null ), but the static checker has no certainty that you didn’t manipulate bar, possibly causing it to be null.

    Also, you should consider that in the future you might have calls to the FooImpl method from methods which do not have a precondition to check whether or not bar is null. You’d want to have the static checker prevent nullreference exceptions from happening in those cases too.

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

Sidebar

Related Questions

Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let me try to explain what I need. I have a server that is
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say you create a wizard in an HTML form. One button goes back,
I have a French site that I want to parse, but am running into
Let's aggregate a list of free quality web site design templates. There are a

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.