schoetbi
schoetbi

Reputation: 12846

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

Upvotes: 4

Views: 698

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273169

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

Upvotes: 1

koenmetsu
koenmetsu

Reputation: 1034

Code Contract does not provide such a feature, nor does C#

Code Contracts only requires from the caller to comply to certain constraints at the start of the called method. These are the so-called preconditions. The postconditions are the responsibility of the callee, and defines what the state of the program will be on exit of the called method.

Design by Contract is a way to define these responsibilities, not to tell callers how they should handle certain conditions caused by the called method.

Upvotes: 2

Related Questions