I'm wondering what ?
means in C# ?
I'm seeing things like: DateTime?
or int?
. I suppose this is specific to C# 4.0?
I can't look for it in Google because I don't know the name of this thing.
The problem is I'm using DateTime and I have a lot of cast errors (from DateTime
to DateTime?
).
Thank you
It's a shorthand for writing Nullable<int>
or Nullable<DateTime>
. Nullables are used with value types that cannot be null (they always have a value).
It is not specific to C#4 by the way.
You can only assign an int?
to an int
if it has a value, so your code would have to do things like:
int? n = 1;
int i = n ?? default(int); //or whatever makes sense
Also note that a Nullable
has two properties, HasValue
and Value
that you can use test if a value has been set and to get the actual value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With