Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, what is the `?` in the type `DateTime?` [duplicate]

Tags:

c#

nullable

I just ran across some code while working with System.DirectoryServices.AccountManagement

public DateTime? LastLogon { get; }

What is the ? after the DateTime for.

I found a reference for the ?? Operator (C# Reference), but it's not the same thing. (280Z28: Here is the correct link for Using Nullable Types.)

like image 531
Scott Chamberlain Avatar asked Jan 15 '10 15:01

Scott Chamberlain


People also ask

What does |= mean in C?

The bitwise OR assignment operator ( |= ) uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable.

What is ?: operator in C?

C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.

What does %d do in C?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

What is -= in C?

This operator is a combination of '-' and '=' operators. This operator first subtracts the value on the right from the current value of the variable on left and then assigns the result to the variable on the left. Example: (a -= b) can be written as (a = a - b) If initially value stored in a is 8.


2 Answers

The ? makes it a nullable type (it's shorthand for the Nullable<T> Structure and is applicable to all value types).

Nullable Types (C#)

Note:

The ?? you linked to is the null coalescing operator which is completely different.

like image 178
Justin Niessner Avatar answered Oct 17 '22 06:10

Justin Niessner


The ? is not an operator in this case, it's part of the type. The syntax

DateTime?

is short for

Nullable<DateTime>

so it declares that LastLogon is a property that will return a Nullable<DateTime>. For details, see MSDN.

The ?? that you linked to is somewhat relevant here. That is the null-coalescing operator which has the following semantics. The expression

x ?? y

evaluates to y if x is null otherwise it evaluates to x. Here x can be a reference type or a nullable type.

like image 43
jason Avatar answered Oct 17 '22 05:10

jason