6

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.)

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431

6 Answers6

27

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.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 4
    I think one of the most important things to point out here is that this is not special to the DateTime type. It works for int, decimal and other usually not-nullable types as well and in the same way. – Anne Schuessler Jan 15 '10 at 15:15
4

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.

jason
  • 236,483
  • 35
  • 423
  • 525
0

shorthand for

Nullable<DateTime>
hackerhasid
  • 11,699
  • 10
  • 42
  • 60
0

It's a shortcut for Nullable<DateTime>. Be aware that methods using these types can't be exposed to COM as they are use generics despite looking like they don't.

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
0

As others have mentioned this ? syntax makes the type nullable.

A couple of key things to keep in mind when working with nullable types (taken from the docs, but I thought it would be helpful to call them out here):

  • You commonly use the read-only properties Value and HasValue when working with nullable types.

e.g.

int? num = null;
if (num.HasValue == true)
{
    System.Console.WriteLine("num = " + num.Value);
}
  • You can use the null coalescing operator to assign a default value to a nullable type. This is very handy.

e.g.

int? x = null;
int y = x ?? -1;
Ian Robinson
  • 16,892
  • 8
  • 47
  • 61
0

As others have mentioned, ? is used for declaring a value type as nullable.

This is great in a couple of situations:

  • When pulling data from a database that has a null value stored in a nullable field (that maps to a .NET value type)
  • When you need to represent "not specified" or "not found".

For example, consider a class used to represent feedback from a customer who was asked a set of non-mandatory questions:

class CustomerFeedback
{
    string Name { get; set; }
    int? Age { get; set; }
    bool? DrinksRegularly { get; set; }
}

The use of nullable types for Age and DrinksRegularly can be used to indicate that the customer did not answer these questions.

In the example you cite I would take a null value for LastLogon to mean that the user has never logged on.

Richard Ev
  • 52,939
  • 59
  • 191
  • 278