What is the proper way to check for null values?

Chev

I love the null-coalescing operator because it makes it easy to assign a default value for nullable types.

 int y = x ?? -1;

That's great, except if I need to do something simple with x. For instance, if I want to check Session, then I usually end up having to write something more verbose.

I wish I could do this:

string y = Session["key"].ToString() ?? "none";

But you can't because the .ToString() gets called before the null check so it fails if Session["key"] is null. I end up doing this:

string y = Session["key"] == null ? "none" : Session["key"].ToString();

It works and is better, in my opinion, than the three-line alternative:

string y = "none";
if (Session["key"] != null)
    y = Session["key"].ToString();

Even though that works I am still curious if there is a better way. It seems no matter what I always have to reference Session["key"] twice; once for the check, and again for the assignment. Any ideas?

BlackBear

What about

string y = (Session["key"] ?? "none").ToString();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the proper way to check multiple form values from the template in Angular?

Is it proper to negate an entire if statement to check for null values?

What's the proper way to check if a constant is defined?

What is the proper way to check if an Iterator is complete?

Proper way to null check in Expression-Tree lambda

What is the proper way to check if a Boolean key exists in NSUserdefaults/UserDefaults

What is the proper way to delay a service worker update check?

What is the proper way to check for existence of variable in an EJS template (using ExpressJS)?

What is the proper way to check for a foreign key with Rails 5.2.4 / rspec 3.12?

What's the proper way to check if JSON has a key?

What is the proper way to check if a document in mongodb with find().limit()?

Multiple check for "None" - what would be the proper way in Python

What is the proper way to check string or list of strings length in JS?

What's the proper way to write hex literal values in Delphi?

What is the proper way to bind to different values in PDO equal to mysqli?

What is the proper way to initialize const containers with different values?

What is a proper way of getting values from a JSON Volley requset?

What would be the proper way to update animation values in a Flutter animation?

What is a proper way to re-order the values of a list inside a list?

What is the best way to check if array has values other than null in php?

Best way to check for null values in Java?

What is best way for object null and empty check?

What is the correct way to check if a cell in a database is null

What is the correct way to check for "possibly null" references?

Proper way to check for URL equality

What's the best way to ensure no null values

what is the alternative way to check for null with out using is null operator in oracle

What is the best way to check if values multiple values in certain ways match?

What is the proper way to wait for connections?