Operator ?

  • Thread starter Thread starter MBSoftware
  • Start date Start date
Well, you have to do that or compare to null with either the cast or
using the Value property.

No. The Value property will throw an exception when HasValue is false.
Anyway, the Value property is the base (non-nullable) type, which can never
be null, so comparison with null is impossible. Same for the result of the
cast.

You must check HasValue before casting or accessing the Value property. My
personal preference (for efficiency) is to check HasValue, then call
GetValueOrDefault() which does not throw, it is just a simple field access.
 
Peter said:
You obviously misunderstood. Let me rephrase my statement so that
you can understand it:

"Whether you are using a cast or the Value property to retrieve the
value, you have to either do that (i.e. check the HasValue property)
or compare to null"


Or check to see if the value is null.

Value can't be null. Value is a non-nullable value type. You can't create
 
Frankly, after the first misunderstanding, I re-read my post and I
agree it wasn't quite as clear as it could have been. But after the
second misunderstanding, it's hard to understand why you can't put in
a little more effort on your own to see how, when interpreted in a
particular way (i.e. the way I _intended_), what I wrote isn't
incorrect.

I know exactly what you're trying to say, but a lot of the people reading
this newsgroup don't have our level of experience with C#, so we have to
write in a way that's correct and clear to them.

If you looked at my remarks *in context*, you would see exactly where the
ambiguity lies (using the word value to refer to something other that the
Value property).

Rather than (your words) "check to see if the value is null" let's just say
"compare the Nullable variable to null", then we obviously are not talking
about the Value property.
 
And in any case, the first post to which you replied, the meaning of
"value" wasn't at issue anyway. The word wasn't even present in the
text you quoted.

Your actual words were "compare to null with either the cast or using the
Value property"

Here the direct object of the verb compare is missing (it wasn't specified
elsewhere in the sentence either), and the various possibilities for "What
is compared to null?" include "the cast" and "the Value property". But you
can't compare either of these to null, as we both very well know.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top