Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of a question mark after a type (for example: int? myVariable)?

Typically the main use of the question mark is for the conditional, x ? "yes" : "no".

But I have seen another use for it but can't find an explanation of this use of the ? operator, for example.

public int? myProperty {    get;    set; } 
like image 396
GenEric35 Avatar asked Apr 22 '10 12:04

GenEric35


People also ask

What does a question mark after a type mean?

It is a shorthand for Nullable<int> . Nullable<T> is used to allow a value type to be set to null . Value types usually cannot be null.

What is question mark after data type C#?

The C# double question mark operator (??) provides a way to handle null values in a more concise and elegant way than the traditional if/else statement. The null coalescing operator was added to C# in version 2.0 along with nullable value types.

What does a question mark in C# mean?

The conditional operator ?: , also known as the ternary conditional operator, evaluates a Boolean expression and returns the result of one of the two expressions, depending on whether the Boolean expression evaluates to true or false , as the following example shows: C# Copy.

What is bool with a question mark?

Answers. It's C# shorthand for Nullable<bool>, which allows a null value to be assigned to a type that is otherwise a value type, and cannot have a null assigned.


2 Answers

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

Nullable types are instances of the System.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<Int32>, pronounced "Nullable of Int32," can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value. A Nullable<bool> can be assigned the values true, false, or null. The ability to assign null to numeric and Boolean types is especially useful when you are dealing with databases and other data types that contain elements that may not be assigned a value. For example, a Boolean field in a database can store the values true or false, or it may be undefined.

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);       }   } } 
like image 196
Sean Avatar answered Oct 20 '22 00:10

Sean


It is a shorthand for Nullable<int>. Nullable<T> is used to allow a value type to be set to null. Value types usually cannot be null.

like image 32
Klaus Byskov Pedersen Avatar answered Oct 19 '22 23:10

Klaus Byskov Pedersen