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.
Related
This question already has answers here:
What does question mark and dot operator ?. mean in C# 6.0?
(3 answers)
Closed 7 months ago.
So I just done it as a habit now but I want to know how it really works. Console.ReadLine()?.ToLower() ?? ""; So why do you put a ? in between .ToLower() and Console.ReadLine(). I know that after the .ToLower() you put ?? ""; to make a null input into a string. But what about the other question mark?
Single question mark (?) means null check.
Basically, if Console.ReadLine is null, the other part will not be implemented.
In other words, Console.ReadLine()?.ToLower() is equal to this
var dummy = Console.ReadLine();
if(dummy != null)
{
dummy.ToLower()....
}
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:
How can I check whether a string variable is empty or null in C#? [duplicate]
(6 answers)
Closed 4 years ago.
I want to handle "" and null while assigning value to the property of the class.
So how can i handle the same. Below is my example which works for null. But also want to handle empty string
Id = characater.Id ?? System.Guid.NewGuid().ToString(),
Use string.IsNullOrEmpty along with the ?: Operator.
Id = string.IsNullOrEmpty(characater.Id)
? System.Guid.NewGuid().ToString()
: characater.Id;
If you you also want to check for white space characters line spaces, line breaks, tabs, you can use String.IsNullOrWhiteSpace instead.
This question already has answers here:
Deep null checking, is there a better way?
(16 answers)
Closed 5 years ago.
currently we have the code as
var val = (returnCode as Code).Element(1).Attribute[2].Value
you can see, the code get the return value, which is a fixed Object, it is very dangerous, could be null reference exception
we could write a lot of if to do the null check, but is there any other gracefully way to handle that ?
If you are afraid of potential null during the evaluation of the expression, use the elvis operator ?. instead of . to securely access properties :
// val will be null if any in the chain is null
var val = (returnCode as Code)?.Element(1)?.Attribute[2]?.Value;
You can also use the ?[ to check array is not null before access an index :
Attribute?[2]
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.