11

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?

dakab
  • 5,379
  • 9
  • 43
  • 67
user2425056
  • 327
  • 2
  • 4
  • 14
  • If you were doing some mathematic calculations based upon a users input, you would want to make sure that they have entered a number so that the equation doesn't use the default value – Sayse May 27 '13 at 13:07
  • 2
    How did you add `nullable` tag beside you don't know what is `?` mean? – Soner Gönül May 27 '13 at 13:09
  • I'm always amazed to see, that when the question is simple, there are lots of duplicate answers. – Nolonar May 27 '13 at 13:10
  • 2
    possible duplicate of [What does "DateTime?" mean in C#?](http://stackoverflow.com/questions/109859/what-does-datetime-mean-in-c) – Sani Huttunen May 27 '13 at 13:14

7 Answers7

21

? is just syntactic sugar, it means that the field is Nullable. It is actually short for Nullable<T>.

In C# and Visual Basic, you mark a value type as nullable by using the ? notation after the value type. For example, int? in C# or Integer? in Visual Basic declares an integer value type that can be assigned null.

You can't assign null to value types, int being a value type can't hold null 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, with Color?[,] you are making an array of nullable Color.

For your question:

The variable 'a' is not initialized. When and where would you use this?

By using ? with variable doesn't make it initialized with null or any other value, it is still has to be initialized.

int? a = null; 

int b = null;//error since b is not nullable
Habib
  • 219,104
  • 29
  • 407
  • 436
4

It means that the type defined is nullable. Check out this link for more info.

With nullables you can do:

int? i = null;

And of course do null checks:

if (i == null)
{
    // do stuff here ...
}
Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
0

This means that the object can have a null-value in Addition to their normal range.

From MSDN:

Nullable types are instances of the 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, pronounced "Nullable of Int32," can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value.
...

Example

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);
        }
    }
}
Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
bash.d
  • 13,029
  • 3
  • 29
  • 42
0

Color? refers to a Nullable Color and is equivalent to Nullable<Color>.

Color c = null;//ERROR Color is a struct, cannot be null!
Color? c = null;//OK

Nullable Types

Ahmed KRAIEM
  • 10,267
  • 4
  • 30
  • 33
0

The syntax T? is shorthand for System.Nullable, where T is a value type. The two forms are interchangeable.

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

Sergey Akopov
  • 1,130
  • 1
  • 11
  • 25
0

Its a Nullable Structure

http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

Also a duplicate https://stackoverflow.com/questions/5407552/what-does-question-mark-after-a-type-name-mean-in-c

Community
  • 1
  • 1
Nipun Ambastha
  • 2,553
  • 1
  • 16
  • 26
0

The ? operator signifies that the value type immediately preceding the operator may have the value null.

One way to think of it, and by no means a formal definition, is its an "exception" to how the value would "normally" be assigned a value.

A classic use case of the operator is in a simple controller in a web app. Suppose we have a typical DB that stores a list of movies. We can access the details of these movies by adding into the URL the following:

.../Movies/Details?MovieName=TopGun

, or,

.../Movies/Details?MovieName=DieHard

But what if we wanted to see the details of a movie where the value of MovieName is not defined? What will you show on your HTML page? Well, this controller will notify us of a bad request:

Check this out:

// GET: Movies/Details/5
public ActionResult Details(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Movie movie = db.Movies.Find(id);
    if (movie == null)
    {
        return HttpNotFound();
    }
    return View(movie);
}
Dean P
  • 1,841
  • 23
  • 23