This question already has answers here:
What do two question marks together mean in C#?
(19 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
I've been seeing statements like this a lot:
int? a = 5;
//...other code
a ??= 10;
What does the ??= mean in the second line? I've seen ?? used for null coalescing before, but I've never seen it together with an equals sign.
It's the same as this:
if (a == null) {
a = 10;
}
It's an operator introduced in C# 8.0: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator
Available in C# 8.0 and later, the null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. The ??= operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.
Related
This question already has answers here:
What is meant by "the null conditional operator short circuits"?
(3 answers)
Closed 3 years ago.
Is it safe to use the . operator after using the null-conditional operator ?. ?
string x = MaybeReturnsNullMethod();
string y = x?.Substring(2).PadRight(1);
I thought the correct code on line 2 to avoid a possible NullReferenceException would be
string y = x?.Substring(2)?.PadRight(1);
The expresssion x?.SomeMethod().ToString() will return null when x is null. This is due to a C# behavior called null propogation.
Even though Substring is called via the null-conditional operator, and
a null value?.Substring could seemingly return null, the language
behavior does what you would want. It short-circuits the call and
immediately returns null, avoiding the programming error that would
otherwise result in a NullReferenceException. This is a concept known
as null-propagation.
It just works.
This question already has answers here:
Why "long value" equals to null is allowed?
(3 answers)
How does comparison operator works with null int?
(5 answers)
Closed 7 years ago.
I've always used the coalescing operator (a.k.a. "really surprised operator, i.e. "??") to get rid of phony nullables (usually fetched from DB as allowing nulls but known to me never to be at that value). It looks like so.
int serious = GetSomeReallyNonNullValue();
int? phony = GetNullableButActuallyNonNullValue();
int result = serious + (phony ?? 0);
However, I just noticed that the below actually compiled. Can't see how it makes sense. And I can't see intuitively if null value will evaluate the expression to true or false...
int? test = null;
if (test < 1337) ;
A lot has been written about "lifting" operations in C# (eg. here), where operators with Nullable<T> arguments are treated as operators on T when all operands are non-null. And null is only equivalent to itself.
usually fetched from DB as allowing nulls but known to me never to be at that value
In which case why is the column not set to not null?
The lifting is there because so many databases have nullable columns when they should not be.
This question already has answers here:
operator << in c# [duplicate]
(5 answers)
Closed 7 years ago.
For example, if I write Console.WriteLine(1<<2<<2+1); in C# console application, the output will be 32
Can you say me why? What does this "<<" operator mean?
Where can I read more about it? I google but couldn't find it
From MSDN:
The left-shift operator (<<) shifts its first operand left by the number of bits specified by its second operand. The type of the second operand must be an int or a type that has a predefined implicit numeric conversion to int.
https://msdn.microsoft.com/en-us/library/a1sway8w.aspx
The number 32 is returned in this case because the addition operator has precedence over the ASHL (<<) operator, but the leftmost ASHL operators are applied first. The expression is evaluated as follows:
1<<2<<2+1
((1<<2)<<(2+1))
((1<<2)<<3)
(4<<3)
32
This question already has answers here:
If the left operand to the ?? operator is not null, does the right operand get evaluated?
(4 answers)
Closed 8 years ago.
The question's title says it all. In a C# expression a ?? b, is b always evaluated, or only when a evaluates to null?
I am curious about this because it might matter in cases when evaluating the right-hand-side expression might have side effects, or when its evaluation might be computationally expensive.
The right-hand side of ?? is lazily evaluated; that is, it gets evaluated only when the expression on the left-hand side has evaluated to null. This can be easily tested:
bool rhsExpressionWasEvaluated = false;
bool _ = (bool?)true ?? (rhsExpressionWasEvaluated = true);
Debug.Assert(!rhsExpressionWasEvaluated);
This question already has answers here:
What do two question marks together mean in C#?
(19 answers)
?? Null Coalescing Operator --> What does coalescing mean?
(7 answers)
Unique ways to use the null coalescing operator [closed]
(16 answers)
Closed 9 years ago.
** Please stop to devote this question. I really don't know what to search for it. I have tried to use keyword like 'C# ??' and tried in the title of the qeustion like '?? what it ' but there is no any question in the 'Questions that may already have your answer' box.
public ICommand _command;
public ICommand Command
{
get
{
return _command ?? (_command = new MvxCommand(AddItem));
}
}
I found i like those code. But I really don't know what does it mean?
Additional, if i want to search in msdn about it, what the keyword I should use for?
It is called a Null coalescing operator
if the first part is null then use the next part, in your case if _command is null then it creates a new command else it would use _command only
it's Null coalescing operator. It means if this has a value use it, if not use the next item.
It's very useful for nullable types and objects.
int? age = null;
var defaultAge = age ?? 21;
//defaultAge is now 21
http://msdn.microsoft.com/en-us/library/ms173224.aspx
Null Coalesce operator. Functions just like, "IsNull" in TSQL. If the left value is not null, use it. Otherwise, use the right value.