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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:42:36+00:00 2026-05-23T02:42:36+00:00

I made a method like this class PersonCollection { [Contracts.CanReturnNull] //dont know if something

  • 0

I made a method like this

class PersonCollection
{
  [Contracts.CanReturnNull]  //dont know if something like this exists?
  IPerson GetPerson(Guid personId)
  {
       if (this.persons.Contains(personId))
            return this.persons[personId];
       else
            return null;
  }
}

Now the calling code needs to handle the null value properly. Is there a way to express a contract for all callers that they need to be able to handle the null value returned by this method?

PersonCollection pc = new PersonCollection();
IPerson p = pc.GetPerson(anyId);
p.Name = "Hugo";  // here I want to have a curly line

What I want is that the p gets marked as potential problematic.

EDIT
I just modified the code and added the calling code and the expcected behaviour. Also I added an attribute that probalbly does not exists on the method GetPerson

  • 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-23T02:42:37+00:00Added an answer on May 23, 2026 at 2:42 am

    What you seem to want (after reading the comments) will happen by default:

    If you enable Code Contracts in the calling code, the verifier will consider that the return of GetPerson() can be null. So:

    IPerson GetPerson(Guid personId)
    {
       // no pre/post conditions
    }
    
    void PrintPerson(IPerson p)
    {
       Contract.Requires(p != null);
       ...
    }
    
    void Foo()
    {
         var p = GetPerson(id);
         PrintPerson(p);    // a warning here: can not verify p != null
    }
    

    And, totally irrelevant to the question, this will usually be more efficient if persons is (like) a Dictionary:

    IPerson GetPerson(Guid personId)
    {
       Person p = null;
    
       this.persons.TryGetValue(personId, out p);
       return p;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a class for DB connection like this: public class DbHelper : IDisposable
When I compile something like this: public class MyClass { void myMethod(String name, String
I made my method with post like this: [OperationContract] [WebInvoke(Method = POST, ResponseFormat =
One can do something like this: class master: @combomethod def foo(param): param.bar() # Param
I made this method + (CGFloat) round: (CGFloat)f { int a = f; CGFloat
This question is similar to this one: Restrict method access to a specific class
I have a class like this : class User { String name; String password;
I've made a very simple REST controller method with Spring 3.0.0.RC1 that uses hibernate
I'm using the PassthruAPP method to hook into HTTP/HTTPS requests made by IE. It's
Made this nice little loop for hiding and showing div's, works as a charm

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.