开发者

Default value of Version class: null or (0,0)?

we identify client software using Version class and use these values extensively, we also store these values in database. How would you store unknown version value? We can get unknown version value if we wasn't able to 开发者_如何学编程successfully query client version.

I ask because I'm fond of Null object pattern, I don't like constant checks for null, and I wish that Version type was a value-type instead of reference-type. But Microsoft implemented Version type as reference-type. At the same time, in .NET v4 they added TryParse() method, which in case of failure saves result as (0,0) but not Null. If you call default constructor without providing exact version value, you would get (0,0) value, and internal fields get initialized to (0, 0, -1, -1). Perhaps, Microsoft realized, that Version should be value-type, who knows :).

But what is the best approach in handling unknown version values? Always check for null, or use default constructor?


I'd be checking for Null. I think (0,0) is misleading in this context.


I would use the "default" value of (0,0) rather than null. As you stated, you won't have to deal with null checks (though, could easily be resolved using null coalescing), it's a value used by the framework as the default value (and failure condition), and I don't know of any situations where having version (0,0) makes sense anywhere. Also I was under the impression that assemblies must always have a version number. So a value of null might not always be available, particularly in an assembly. Or at least if this value is for your benefit, you should follow the pattern used by the framework.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜