11

What does ? mean:

public bool? Verbose { get; set; }

When applied to string?, there is an error:

The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable'

abatishchev
  • 98,240
  • 88
  • 296
  • 433
MicMit
  • 2,372
  • 5
  • 28
  • 41
  • 3
    Just to append to the other answers describing nullable types - a bool is stored in memory as a 0 or 1, there is no other option. By making it a nullable type allocates a larger area of memory where its value can be true,false or null. You'll notice that you can do things like Verbose.HasValue now which you couldn't do if it was defined as a bool. – David Neale May 19 '10 at 08:47
  • Also to add to the general conversation, all reference types are nullable by default (I.E. you can set them all to null), so the Nullable type is designed to not accept them, since it would duplicate functionality and complicate matters. – Rangoric May 19 '10 at 09:05
  • 1
    possible duplicate of [What does "DateTime?" mean in C#?](http://stackoverflow.com/questions/109859/what-does-datetime-mean-in-c) – Jørn Schou-Rode May 24 '10 at 14:57

7 Answers7

18

? makes your non-nullable (value) types nullable. It doesn't work for string, as it is reference type and therefore nullable by default.

From MSDN, about value types:

Unlike reference types, a value type cannot contain the null value. However, the nullable types feature does allow for values types to be assigned to null.

? is basically a shorthand for Nullable<T> structure.

If you want to know more, MSDN has a great article regarding this topic.

Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126
11

The ? is shorthand for the struct below:

struct Nullable<T>
{
    public bool HasValue;
    public T Value;
}

You can use this struct directly, but the ? is the shortcut syntax to make the resulting code much cleaner. Rather than typing:

Nullable<int> x = new Nullable<int>(125);

Instead, you can write:

int? x = 125;

This doesn't work with string, as a string is a Reference type and not a Value type.

Neil Knight
  • 47,437
  • 25
  • 129
  • 188
7

bool? is a short form for System.Nullable<bool>. Only value types are accepted for the type parameter and not reference types (like e.g. string).

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
5

bool? is a shorthand notation for Nullable<bool>. In general, the documentation states:

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

Since string is not a value type (it's a reference type), you cannot use it as the generic parameter to Nullable<T>.

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
3

The ? operator indicates that the property is in fact a nullable type.

public bool? Verbose { get; set; } 

is equilvalent to

public Nullable<bool> Verbose { get; set; }

A nullable type is a special type introduced in c# 2.0 which accepts a value type as a generic praramater type and allow nulls to be assigned to the type.

The nullable type only accept value types as generic arguments which is why you get a compile error when you try to use the ? operator in conjunction with the string type.

For more information: MSDN Nullable Types

Beansy
  • 71
  • 5
3

Only value types can be declared as Nullable. Reference types are bydefault nullable. So you cannot make nullable string since string is a reference type.

this. __curious_geek
  • 42,787
  • 22
  • 113
  • 137
3

the ? means that your value type can have a null value, specially in the case of database

handling you need these nullables to check if some value is null or not.

It can be applied to only value types coz reference types can be null .

Embedd_0913
  • 16,125
  • 37
  • 97
  • 135