我写了一个类似这样的方法:
我希望的是将p标记为潜在问题。 编辑:我刚刚修改了代码,并添加了调用代码和预期行为。此外,我还添加了一个可能不存在于GetPerson方法上的属性。
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;
}
}
现在调用代码需要正确处理空值。是否有一种方法可以表达一个契约,要求所有调用方都能够处理此方法返回的空值?
PersonCollection pc = new PersonCollection();
IPerson p = pc.GetPerson(anyId);
p.Name = "Hugo"; // here I want to have a curly line
我希望的是将p标记为潜在问题。 编辑:我刚刚修改了代码,并添加了调用代码和预期行为。此外,我还添加了一个可能不存在于GetPerson方法上的属性。
null
。除非GetPerson
通过合同确保IPerson
不为null,否则调用者必须处理可能返回null
的情况。 - Peter K.