What does question mark and dot operator ?. mean in C# 6.0?
It's the null conditional operator. It basically means:
"Evaluate the first operand; if that's null, stop, with a result of null. Otherwise, evaluate the second operand (as a member access of the first operand)."
In your example, the point is that if a
is null
, then a?.PropertyOfA
will evaluate to null
rather than throwing an exception - it will then compare that null
reference with foo
(using string's ==
overload), find they're not equal and execution will go into the body of the if
statement.
In other words, it's like this:
string bar = (a == null ? null : a.PropertyOfA);
if (bar != foo)
{
...
}
... except that a
is only evaluated once.
Note that this can change the type of the expression, too. For example, consider FileInfo.Length
. That's a property of type long
, but if you use it with the null conditional operator, you end up with an expression of type long?
:
FileInfo fi = ...; // fi could be null
long? length = fi?.Length; // If fi is null, length will be null
what is ? operator in NotifyPropertyChanged
This is the syntax for null propagation which was introduced in c# 6.0. Here the ?
is the null-conditional operator. This code is equivalent to:
if(!(PropertyChanged is null))
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
c# dynamic variable with ?? and ? operator
It essentially means
if name is not null
set name to name
else
if data is null
set name to null
else
set name to data.name
The second operator (?.
) avoids a NullReferenceException
by returning null instead of trying to use the access modifier. The first (??
) returns the first operand if the value is not null, otherwise it returns the second.
Note that neither of these are specific to dynamic
or Azure.
It could also have been written as
if ((name == null) && (data != null))
{
name = data.name;
}
Check null/empty at multiple levels using question mark operator c#
Use the following:
return dummy?.list?.FirstOrDefault()?.A;
You should avoid using index on a List as Lists aren't deterministic. If you have to know the position of an item in an a collection, use an array.
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 do two question marks together mean in C#?
It's the null coalescing operator, and quite like the ternary (immediate-if) operator. See also ?? Operator - MSDN.
FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();
expands to:
FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper();
which further expands to:
if(formsAuth != null)
FormsAuth = formsAuth;
else
FormsAuth = new FormsAuthenticationWrapper();
In English, it means "If whatever is to the left is not null, use that, otherwise use what's to the right."
Note that you can use any number of these in sequence. The following statement will assign the first non-null Answer#
to Answer
(if all Answers are null then the Answer
is null):
string Answer = Answer1 ?? Answer2 ?? Answer3 ?? Answer4;
Also it's worth mentioning while the expansion above is conceptually equivalent, the result of each expression is only evaluated once. This is important if for example an expression is a method call with side effects. (Credit to @Joey for pointing this out.)
What does question mark and dot operator ?. mean in C# 6.0?
It's the null conditional operator. It basically means:
"Evaluate the first operand; if that's null, stop, with a result of null. Otherwise, evaluate the second operand (as a member access of the first operand)."
In your example, the point is that if a
is null
, then a?.PropertyOfA
will evaluate to null
rather than throwing an exception - it will then compare that null
reference with foo
(using string's ==
overload), find they're not equal and execution will go into the body of the if
statement.
In other words, it's like this:
string bar = (a == null ? null : a.PropertyOfA);
if (bar != foo)
{
...
}
... except that a
is only evaluated once.
Note that this can change the type of the expression, too. For example, consider FileInfo.Length
. That's a property of type long
, but if you use it with the null conditional operator, you end up with an expression of type long?
:
FileInfo fi = ...; // fi could be null
long? length = fi?.Length; // If fi is null, length will be null
Related Topics
How to Easily Check If Access Is Denied For a File in .Net
Ado.Net |Datadirectory| Where Is This Documented
How to Make a Winforms App Go Full Screen
String Output: Format or Concat in C#
How to Use Optional Parameters in C#
Display Lines Number in Stack Trace For .Net Assembly in Release Mode
Maximum Number of Threads in a .Net App
Split a Collection into 'N' Parts With Linq
Is Floating-Point Math Consistent in C#? Can It Be
How to Connect to MySQL Database
Catch Multiple Exceptions At Once