开发者

Representing search failure with a non-nullable type

I have a method that searches a list of objects based on some of the fields of the object. If a matching object is found, I return it, but I want to be able to represent a no-m开发者_如何学Pythonatch situation. Normally I'd return null but I'm working with a non-nullable class I cannot change.


There are several options. Use a Nullable<T>, or return a bool and use an out parameter to get the actual result, e.g.:

MyType? FindObject() { }

Or:

bool FindObject(out MyType result) { }


This situation can be handled by Null Pattern.

What confuse me is that you wrote that you return list of objects and then object. Could you give some details ?


Can you throw an exception? NoObjectFoundException

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜