Search code examples
c#-4.0code-contractsdesign-by-contract

Code Contracts in C# 4.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


Solution

  • 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;
    }