3

i have seen the following code in an c# example:

public void AddScreen(GameScreen screen, PlayerIndex? controllingPlayer)
{
    screen.ControllingPlayer = controllingPlayer;
    screen.ScreenManager = this;
    screen.IsExiting = false;
}

and i have no clue what the ? is doing after PlayerIndex, it is an enum, and in the class every notice of it is with the ? behind it. my question: what does it do, what is it called and why would you use it.

I have googled this, but it didn't get me far since i dont know the name of this coding and google filters out the question mark in the search query

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
MooshBeef
  • 279
  • 1
  • 5
  • 15

8 Answers8

9

The ? makes PlayerIndex a NULLABLE type.

That way controllingPlayer can be NULL even if it is an enum or a basic type like int.

juergen d
  • 201,996
  • 37
  • 293
  • 362
3

The ? is a nullable type. This means that controllingPlayer can contain null or a value.

To check whether there is a value associated with the variable, you can use HasValue. To retrieve the actual value, use Value

if ( controllingPlayer.HasValue )
    // now do something with controllingPlayer.Value
Steve
  • 7,171
  • 2
  • 30
  • 52
3

The question mark denotes that PlayerIndex is treated as a nullable type.

Probably PlayerIndex is not a class or struct but an enum or alias for int or something like that. If it's an alias, you should find something like this in the code:

using PlayerIndex= System.Int32;
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
1

It's short for Nullable<PlayerIndex> which means that you can pass a PlayerIndex value or null.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
1

The ? is indicating that controllingPlayer can be null. It could also be written as Nullable<PlayerIndex> controllerPlayer.

This is useful when working with valuetypes which can not be null, like reference types can be. If you have a regular int, you cannot differentiate between a variable that is given the value 0 and a variable that is never written to. By wrapping it in a Nullable<>, you can now check if it has a value or not:

int notNullable;  //will be initialized to 0 by default.
int? nullable;    //will be initialized to null by default.

if (nullable.HasValue)        //Do something if the variable has been given a value
{

   return nullable.Value;     //get the actual int-value
}

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

Vegar
  • 12,828
  • 16
  • 85
  • 151
0

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

fiberOptics
  • 6,955
  • 25
  • 70
  • 105
0

Appending ? to a type when defining something it is shorthand for wrapping in a Nullable<T> (see Nullable Types for further details). This means that value types, usually not able to express nullity, can be checked for a non-value instead of always resorting to their default (i.e., an int, rather than default to 0, will default to null).

You may check whether or not the thing has a value, and access the value thusly:

var hasValue = nullableThing.HasValue;

var underlyingValue = nullableThing.Value;
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
0

PlayerIndex? indicate that varible can be null value. The syntax T? is shorthand for System.Nullable, where T is a value type. please refer to Nullable Types (C# Programming Guide).

Habibillah
  • 27,347
  • 5
  • 36
  • 56