Reputation: 2770
I was looking through some code the other day and I saw something like (int?) and I dont think Ive ever see that before. What does it mean when you use a ? after a type?
Upvotes: 2
Views: 290
Reputation: 108880
It is short for Nullable<T>
.
This is a generic struct that can wrap a value-type to add the value null
. To make the use of this type more convenient C# adds quite a bit of compiler-magic. Such as the short-name T?
, lifted operators,...
The following thread on SO is interesting too: ? (nullable) operator in C#
Upvotes: 13
Reputation: 116
It's a variation/alternative of the Nullable<Type>
. Have seen it used a lot with DateTime to avoid the default DateTime value which gives an error in DB columns related to dates. Quite useful actually.
Upvotes: 6
Reputation: 59022
That is the shorthand syntax for Nullable<T>
(or in your case Nullable<int>
).
This is used when you need value types to be null, such as int
, Boolean
and DateTime
.
Upvotes: 4
Reputation: 36111
As other people have said int?
is short for Nullable<int>
.
This article is a couple of years old now but it's a good explanation of nullable types
Upvotes: 3
Reputation: 5681
the ? after the type implies that the type can have null
value besides its normal values.
I've seen the use mostly for database related types where you have Nullable
columns
Upvotes: 3