1

Possible Duplicate:
? (nullable) operator in C#

What does the ? do used like this:

class Test
{
    public int? aux;
}

It's probably a simple question for someone more familiar with c#, but I don't really know what to search for. I read an explanation but didn't fully understand it. I would also be interested in knowing what the "??" operator does. Some examples on where they would be useful would be of great help.

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 1
    As for `??`, there has been several questions on StackOverflow about it. – leppie Sep 06 '11 at 06:22
  • 1
    I do realize there are a lot of possible duplicates, but I couldn't really search for it. Looking for nullable would of course have worked, only I didn't know it was called a nullable operator. – Luchian Grigore Sep 06 '11 at 06:27

5 Answers5

5

This (int?) is shorthand for Nullable<int>.

leppie
  • 115,091
  • 17
  • 196
  • 297
  • Can you give an example of where and why it should be used? – Luchian Grigore Sep 06 '11 at 06:33
  • @Luchian Grigore Nullables are used when you want to store null into int data type. By default you cant store null in valuetypes which are not nullable. Say, your searching in a collection of ints and in case if you cant find any thing, then just return null saying nothing was found. Or some third party API returns null in int type. – Zenwalker Sep 06 '11 at 06:58
3

It can be any int value plus an additional null. See more info Nullable Types

The purpose of using Nullable int is while often using database operations, there are conditions where some value might be null and we have to express in code. For Example consider the folowing schema

Employee

 - id,  bigint, not null
 - name,  nvarchar(100), null
 - locationId,  bigint, null

Suppose locationId of an employee is not available, so in database the value of locationid of employee is NULL. On C# side, you know that int can not have a NULL value, so type, Nullable int(and few others) has been added, due to which we can easily show that locationId has no value.

Adeel
  • 19,075
  • 4
  • 46
  • 60
2

"?" denotes a nullable type

Ovidiu Pacurar
  • 8,173
  • 2
  • 30
  • 36
1

int? means nullable integer: aux can have an int value or be null!!

Marco
  • 56,740
  • 14
  • 129
  • 152
1

As many others have already answered, the ? denotes nullable types. This means that a variable of type int? can be assigned null in addition to an integer values.
This is used when you want to distinguish the case of a variable which has not been initialized from the case in which it has been assigned the default value.
For instance you can consider the case of bool. When you declare a bool variable, it has the false value by default. This means that when its value is false you can't tell if this happens because someone has assigned the variable that value, of because nobody touched it. In this case a bool? is useful since it allows you to distinguish a "real" false value (explicitely assigned by some code) from the not initailized case.

Francesco Baruchelli
  • 7,320
  • 2
  • 32
  • 40