Leroy Jenkins
Leroy Jenkins

Reputation: 2770

What is (<type>?)?

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

Answers (7)

CodesInChaos
CodesInChaos

Reputation: 108880

It is short for Nullable<T>.

Nullable types in C#

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

gnikolaropoulos
gnikolaropoulos

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

alexn
alexn

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

Fermin
Fermin

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

CheeZe5
CheeZe5

Reputation: 993

int? is syntactic sugar for Nullable<int>.

Upvotes: 5

Maverik
Maverik

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

Andrey
Andrey

Reputation: 6204

it means Nullable, so our value-type variable can be null

Upvotes: 3

Related Questions