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]
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:
C# null check chain in method call
(4 answers)
Closed 3 years ago.
How Can I check null for each object I am using in below chain?
forensicId = Message.Events.SMS.SMS_Mappings.FirstOrDefault().Bug.ForensicId;
More details: I want to access ForensicId from (tables/Proxies loaded by entity framework) BUG which is part of a SMS_Mappings and SMS_Mappings are again part of some table.
Is there any way where I can check if Message is not null or of events are not null and SMS is not null and so on within a single line.
Try this forensicId = Message?.Events?.SMS?.SMS_Mappings?.FirstOrDefault()?.Bug?.ForensicId; It returns null if any object in chain is null or ForensicId value if everything is ok. There is a nice article about such scenarios
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:
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.