Check null within chain of object accessing [duplicate] - c#

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

Related

foreach iterator null when looping through collection [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 2 years ago.
I am using Visual Studio 2017 with the ASP.NET Core 2.1 Framework. When I try and loop through a static object I have in my controller, the iterator seems to be null.
The static object I am calling definitely has a collection seen below:
However When I try and see what the details are of the iterator I get a null reference exception even through the iterator should have a value:
What would be the cause of this null reference?
EDIT: Thanks to the answer I was able to debug what the issue was.
In the code where I am setting the bool wasFound I am doing a where query against a list of objects where the name of the incoming connection is in the list. In this case, the connection name was null and so it would fail.
Obviously the null reference error was a bit vague as to what was causing the error.
The problem is not with i itself, most probably the incoming instance is null, you can use ?. operator in this case:
i?.incoming?.connection

ASP.NET MVC correct way to use values that may or may not have the value of null? [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 3 years ago.
What is the correct way to use values that may or may not have the value of null?
I have this piece of code that sends an email to a manager if the employee who is taking a holiday request has a manager.
if(holidayRequestForm.Employee.Site.SiteManagerEmail != null)
{
SendMailToManager();
}
However this is causing a NullReferenceException.
What would be the correct way to implement calling the SendMailToManager() if that Employee has one without causing a NullReferenceException.
Is it bad practise to use possible null values this way?
Take a look at Null-conditional operator ?. in the docs in order to understand how you can rewrite your if statement (avoiding NullReference exceptions).
if(holdayRequestForm?.Employee?.Site?.SiteManagerEmail != null)
{
SendMailToManager();
}
I don't find it a bad practice. I do not think you have any other possibilities in the described situation.

Entity Framework: What is use /Meaning of (?) question mark [duplicate]

This question already has answers here:
What does question mark and dot operator ?. mean in C# 6.0?
(3 answers)
Closed 4 years ago.
I am new to my project, in the project they used Entity Framework & LINQ to
manipulate the DB data. Here I am not able to understand why the question mark (?) is used after get() method in the following query. Is it possible to use the (?) mark as like below ?. If yes, then could you please explain in detail.
uOW.ApplicationDetailsRepository.Get()?
.Where (x=>x.Name=="SomeConditions").Tolist();
When the question mark is used this way (in combination with the period: ?.), it is called the Null-conditional operator. It is a check for null before attempting the rest of the statement.
Here is a reference explaining in detail.

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 DBNull.Value exist [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the point of DBNull?
I have recently been working on a number of sections of code that deal with the insertion of Nullable types into a database.
As I'm sure anyone who has dealt with similar code will be aware of the annoyance of constantly writing conditional logic to deal with the insertion of nulls into a database
IE:
MyValue.HasValue ? MyValue.Value : DBNull.Value;
If(MyValue.HasValue, MyValue.Value, DBNull.Value)
Basically I am just wondering if someone here could be kind enough to explain why DBNull.Value exists and why Null simply couldn't be used?
Here you have one explanation
http://codebetter.com/petervanooijen/2004/04/12/system-dbnull-value-null/
If you call ExecuteScalar and you get null, then it means that no data was found in the database, but if you get DBNull that means you found data but the value was actually null.

Categories