I have some C# code with the following array declaration. Note the single ?
after Color
.
private Color?[,] scratch;
In my investigating I have found that if you you have code such as:
int? a;
The variable a
is not initialized. When and where would you use this?
Best Answer
?
is just syntactic sugar, it means that the field is Nullable. It is actually short forNullable<T>
.You can't assign
null
to value types,int
being a value type can't holdnull
value,int?
on the other hand can store null value.Same is the case with
Color
, since it is a structure (thus value type) and it can't hold null values, withColor?[,]
you are making an array of nullable Color.For your question:
By using
?
with variable doesn't make it initialized withnull
or any other value, it is still has to be initialized.