Coder Perfect

What does a question mark following a type (for instance, int? myVariable) mean?

Problem

The conditional, x?, is the most common application of the question mark. “Yes” vs. “No.”

However, I’ve seen another application for it, but I can’t seem to locate an explanation for it, such as with the? operator.

public int? myProperty
{
   get;
   set;
}

Asked by GenEric35

Solution #1

This indicates that the value type in question is nullable.

Answered by Sean

Solution #2

Nullableint> is a shorthand for Nullable. NullableT> is a type that allows you to set a value type to null. In most cases, value types cannot be null.

Answered by Klaus Byskov Pedersen

Solution #3

In

x ? "yes" : "no"

The? denotes an if condition. The boolean condition is represented by x, and the part before the: is the then phrase, while the part after is the else sentence.

In, for example,

int?

The? specifies a nullable type, implying that the type preceding it may have a null value.

Answered by eKek0

Solution #4

The type is declared to be nullable.

Answered by Thanos Papathanasiou

Solution #5

practical usage:

public string someFunctionThatMayBeCalledWithNullAndReturnsString(int? value)
{
  if (value == null)
  {
    return "bad value";
  }

  return someFunctionThatHandlesIntAndReturnsString(value);
}

Answered by A.J.Bauer

Post is based on https://stackoverflow.com/questions/2690866/what-is-the-purpose-of-a-question-mark-after-a-type-for-example-int-myvariabl