What is the purpose of a question mark after a type (for example: int? myVariable)?

C#asp.net.Net

C# Problem Overview


Typically the main use of the question mark is for the conditional, x ? "yes" : "no".

But I have seen another use for it but can't find an explanation of this use of the ? operator, for example.

public int? myProperty
{
   get;
   set;
}

C# Solutions


Solution 1 - C#

It means that the value type in question is a nullable type

> Nullable types are instances of the System.Nullable struct. A > nullable type can represent the correct range of values for its > underlying value type, plus an additional null value. For example, a > Nullable<Int32>, pronounced "Nullable of Int32," can be assigned any > value from -2147483648 to 2147483647, or it can be assigned the null > value. A Nullable<bool> can be assigned the values true, false, or > null. The ability to assign null to numeric and Boolean types is > especially useful when you are dealing with databases and other data > types that contain elements that may not be assigned a value. For > example, a Boolean field in a database can store the values true or > false, or it may be undefined. > > class NullableExample > { > static void Main() > { > int? num = null; >
> // Is the HasValue property true? > if (num.HasValue) > { > System.Console.WriteLine("num = " + num.Value); > } > else > { > System.Console.WriteLine("num = Null"); > } >
> // y is set to zero > int y = num.GetValueOrDefault(); >
> // num.Value throws an InvalidOperationException if num.HasValue is false > try > { > y = num.Value; > } > catch (System.InvalidOperationException e) > { > System.Console.WriteLine(e.Message); > } > } > }

Solution 2 - C#

It is a shorthand for Nullable<int>. Nullable<T> is used to allow a value type to be set to null. Value types usually cannot be null.

Solution 3 - C#

In

x ? "yes" : "no"

the ? declares an if sentence. Here: x represents the boolean condition; The part before the : is the then sentence and the part after is the else sentence.

In, for example,

int?

the ? declares a nullable type, and means that the type before it may have a null value.

Solution 4 - C#

it declares that the type is nullable.

Solution 5 - C#

practical usage:

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

  return someFunctionThatHandlesIntAndReturnsString(value);
}

Solution 6 - C#

int? is shorthand for Nullable<int>. The two forms are interchangeable.

Nullable<T> is an operator that you can use with a value type T to make it accept null.

In case you don't know it: value types are types that accepts values as int, bool, char etc...

They can't accept references to values: they would generate a compile-time error if you assign them a null, as opposed to reference types, which can obviously accept it.

Why would you need that? Because sometimes your value type variables could receive null references returned by something that didn't work very well, like a missing or undefined variable returned from a database.

I suggest you to read the Microsoft Documentation because it covers the subject quite well.

Solution 7 - C#

Means that the variable declared with (int?) is nullable

int i1=1; //ok
int i2=null; //not ok

int? i3=1; //ok
int? i4=null; //ok

Solution 8 - C#

To add on to the answers above, here is a code sample

struct Test
{
    int something;
}
struct NullableTest
{
    int something;
}
class Example
{
    public void Demo()
    {
        Test t = new Test();
        t = null;

        NullableTest? t2 = new NullableTest();
        t2 = null;
    }
}

This would give a compilation error:

> Error 12 Cannot convert null to 'Test' because it is a non-nullable > value type

Notice that there is no compilation error for NullableTest. (note the ? in the declaration of t2)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGenEric35View Question on Stackoverflow
Solution 1 - C#SeanView Answer on Stackoverflow
Solution 2 - C#Klaus Byskov PedersenView Answer on Stackoverflow
Solution 3 - C#eKek0View Answer on Stackoverflow
Solution 4 - C#Thanos PapathanasiouView Answer on Stackoverflow
Solution 5 - C#A.J.BauerView Answer on Stackoverflow
Solution 6 - C#Nicola AmadioView Answer on Stackoverflow
Solution 7 - C#lucaspompeunView Answer on Stackoverflow
Solution 8 - C#Sunil PurushothamanView Answer on Stackoverflow