Null conditional followed by dot operator C# [duplicate] - c#

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.

Related

What is the double question mark equals sign (??=) in C#? [duplicate]

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.

handling null and string.empty using ?? operator in C# [duplicate]

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.

in C#, how to gracefully handle the null check [duplicate]

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]

Why does it make sense in C# to compare nullable and non-nullable int? [duplicate]

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.

In c# , what does it mean '??'? [duplicate]

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.

Categories