How can I show that a method will never return nul

2020-08-18 08:36发布

I have a method which never returns a null object. I want to make it clear so that users of my API don't have to write code like this:

if(Getxyz() != null)
{
  // do stuff
}

How can I show this intent?

9条回答
ら.Afraid
2楼-- · 2020-08-18 08:54

Document it and provide the source code.

查看更多
地球回转人心会变
3楼-- · 2020-08-18 08:57

Either document it well (maybe with .NET standard documentation system) or you have to use some Contract API. Here are some: http://geekswithblogs.net/Podwysocki/archive/2008/01/22/118770.aspx http://www.codeproject.com/KB/cs/designbycontract.aspx

查看更多
疯言疯语
4楼-- · 2020-08-18 09:05

I like to think of it the other way around: if my function could return a null value I better make sure that the function's user knows about it.

查看更多
登录 后发表回答