What is the purpose of a question mark after a value type (for example: int? myVariable)?
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. ANullable<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);
}
}
}
What does a question mark after a reference type mean in C#?
In C# 8.0, nullable reference types were introduced - this allows you to mark reference types as nullable by appending ?
to the type name.
In null-state analysis, the code is statically checked to keep track of the null-state of references. This analysis can result in one of the 2 states below:
not-null - variable has a non-null value
maybe-null - variable can be null or not-null; the analyser can't determine for sure
This is useful as it helps the compiler provide warnings to help you write code less prone to NullReferenceExceptions
and more consumable by other APIs.
For example, the compiler will emit a warning for the below.
#nullable enable
...
string message = null;
// Warning - CS8602 Dereference of a possibly null reference
Console.WriteLine(message.Length);
This is because since message
was set to null, its state was tracked as maybe-null. This throws a warning to alert you just in case you're trying to access the properties of a null object. And yes, you are.
Here's a different example:
#nullable enable
...
string message = "The quick brown fox jumps over the lazy dog";
// No warning
Console.WriteLine(message.Length);
Since message
is known to not be null, its state is set to not-null. The compiler won't produce a warning as it knows that you can dereference the variable safely. It is definitely not null.
Alternatively, you can also just decide to tell the world that some variables can be null :)
Question mark '?' given in arguments of a method
Simple, this isn’t C++ code. It’s C#, where ?
denotes a nullable value type. I.e. a value type that can also be null
. int?
is a shortcut for Nullable<int>
.
In FLUTTER / DART, why do we sometimes add a question mark to "String" when declaring a variable?
variable_type ? name_of_variable;
means that name_of_variable can be null.
variable_type name_of_variable1;
means that name_of_variable1 cannot be null and you should initialize it immediately.
late variable_type name_of_variable2;
means that name_of_variable2 cannot be null and you can initialize it later.
late variable_type name_of_variable3;
variable_type ? name_of_variable4;
name_of_variable4=some_data;
name_of_variable3=name_of_variable4!;// name_of_variable4! means that you are sure 100% it never will be null
Real example with int type:
int ? a=null; // OK
int b=null; // b cannot be equal null
late int c =null; // c cannot be equal null
late int d;
d=5; //OK
late int d; // if you never initialize it, you ll got exception
int e=5;
int? f=4;
int ? z;
e=f!; // OK, You are sure that f never are null
e=z!;// You get exception because z is equal null
What does one question mark following a variable declaration mean?
This is a nullable type. Nullable types allow value types (e.g. int
s and structures like DateTime) to contain null.
The ?
is syntactic sugar for Nullable<DateTime>
since it's used so often.
To call ToString()
:
if (timstamp.HasValue) { // i.e. is not null
return timestamp.Value.ToString();
}
else {
return "<unknown>"; // Or do whatever else that makes sense in your context
}
Question Mark (?) after session variable reference - What does that mean
It performs a null-check on Session("LightBoxID")
before attempting to call .ToString()
on it.
MS Docs: Null-conditional operators ?.
and ?[]
Question mark on a type
Primitive types such as integers and booleans cannot generally be null, but the corresponding nullable types (nullable integer and nullable boolean, respectively) can also assume the NULL value. NULL is frequently used to represent a missing value or invalid value, such as from a function that failed to return or a missing field in a database, as in NULL in SQL.
Source : http://en.wikipedia.org/wiki/Nullable_type
Nullable types in C# - http://msdn.microsoft.com/en-us/library/vstudio/1t3y8s4s.aspx
What does class? (class with question mark) mean in a C# generic type constraint?
It enforces that T has to be a nullable reference type.
The type you set in for T, must derive from object?
.
It's a new feature in C#8, to explictly declare a type as nullable.
if you have
Add<T>(T tmp);
You document, it's OK to Add null;
Related Topics
Return Json, But It Includes Backward Slashes "\", Which I Don't Want
Passing a List of Objects into an MVC Controller Method Using Jquery Ajax
C# How to Split a List in Two Using Linq
Casting an Object into the Same Type as an Ienumerable Collection
Reference External Dll in .Net Core Project
How to Make Xmlserializer Ignore the Namespace on Deserialization
Convert Character to Its Alphabet Integer Position
C# Double - Tostring() Formatting With Two Decimal Places But No Rounding
Model Id Property Null in ASP.NET MVC C#
In C# What Is the Default Value of the Bytes When Creating a New Byte Array
How to Call Another Controller Action from a Controller in MVC
Merge Multiple Lists into One List With Linq
How to Loop Through Each and Every Row, Put Value in Gridview Column Cells
What Is the Purpose of a Question Mark After a Type (For Example: Int Myvariable)