Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# What is the '?' operator [duplicate]

Tags:

c#

.net

Possible Duplicate:
What does the bool? return type mean?

I came a cross the following property in a class

public long? EmployeeId { get; set; }

I googled this operator with no luck, according to the MSDN MSDN OPERATOR there is only the operators ?? null-coalescing operator and ?: conditional operator. but what about ?

like image 296
Maro Avatar asked Feb 02 '13 18:02

Maro


4 Answers

In this case ? is not an operator. It is a shorter way to write: Nullable<long>

T? is exactly the same as Nullable<T> (with T a type)

It is called a nullable type (see MSDN)

It is used to allow "non-nullable" type (like int, long, a struct) to be assigned a null value.

It is useful when you need a possible invalid state for a value type, or if the data is being retrieved from a database that may contain a null value.

like image 166
Cédric Bignon Avatar answered Sep 20 '22 08:09

Cédric Bignon


This is not an operator. This is a shorthand for the Nullable type.

like image 31
Igal Tabachnik Avatar answered Sep 22 '22 08:09

Igal Tabachnik


It's a nullable type. Look here:

http://msdn.microsoft.com/en-us/library/1t3y8s4s(v=vs.80).aspx

like image 37
Florin Petriuc Avatar answered Sep 23 '22 08:09

Florin Petriuc


The syntax T? is shorthand for System.Nullable, where T is a value type. The two forms are interchangeable.

Source: Look here

like image 37
WooCaSh Avatar answered Sep 23 '22 08:09

WooCaSh