开发者

In C#, if I am returning a Point and cannot find the coordinate what should I return?

Firstly it is not an exception since it is part of the normal flow开发者_如何学Go of operations. I tried to return a null object but this was not allowed. I can get by returning a Point with negative coordinates but it feels like a hack. For example if I return (-1, -1) then instead of checking for a negative number, conceivably some use of my library might directly check for equality with -1 which will then break if my internal implementation changes


You could return a nullable Point by changing your return type to Point?. That way you can also return null.

Otherwise you can create a static Point instance somewhere with (−1, −1) and always compare against that. PointUtils.InvalidPoint is probably nicer to read than p.x == -1 && p.y == -1.


You should change the function's return type to Point?, then return null.


How about Point.Empty? http://msdn.microsoft.com/en-us/library/system.drawing.point.empty%28VS.71%29.aspx


If it doesn't bite you afterwards (i.e. if your points cannot be 0,0): Point.Empty.


You could change the method to take an out bool isValid parameter. If isValid is false, the user would know to ignore the Point result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜