Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1 linear, beauty and clean way to assign value if null in C#?

Tags:

c#

null-check

Before you rush to think about the ?? null coalescing operator:

string result = myParent.objProperty.strProperty ?? "default string value if strObjProperty is null";

The problem here is when whether myParent or objProperty are null, then it will throw an exception before even reaching the evaluation of strProperty.

To avoid the following extra null checkings:

if (myParent != null)
{
   if (objProperty!= null)
   {
       string result = myParent.objProperty.strProperty ?? "default string value if strObjProperty is null";
   }
}

I generally use something like this:

string result = ((myParent ?? new ParentClass())
                .objProperty ?? new ObjPropertyClass())
                .strProperty ?? "default string value if strObjProperty is null";

So if the object is null then it creates a new one only to be able to access the property.

Which is not very clean.

I would like something like a '???' operator:

string result = (myParent.objProperty.strProperty) ??? "default string value if strObjProperty is null";

... which would survive whatever "null" from inside the parenthesis to return the default value instead.

Thanks for your tips.

like image 346
renzol Avatar asked Apr 29 '16 09:04

renzol


People also ask

How to assign a null value to a variable in C++?

This is very simple to assign a null value to the variable in C++; we just need to do this at the time of initialization only. This variable then turns to be treated as the Null pointer. Below see the syntax to understand this better and used while programming see below;

How to avoid null in C++?

This article is written for C only. C++ handles NULL somewhat differently, and has the option to avoid it entirely by using references instead of pointers. Thanks! wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors.

What is the value of 0 in C++?

As far as your source code is concerned, 0 (or any integral expression that evaluates to 0) represents a null pointer. Both C and C++ define the NULL macro as the null pointer constant. When your code is compiled, the null pointer constant will be replaced with the appropriate null pointer value in the generated machine code.

Why null values are a big problem in sklearn?

Null values are a big problem in machine learning and deep learning. If you are using sklearn, TensorFlow, or any other machine learning or deep learning packages, it is required to clean up null values before you pass your data to the machine learning or deep learning framework. Otherwise, it will give you a long and ugly error message.


1 Answers

What about the null propagation operator, which comes with C# 6?

string result = (myParent?.objProperty?.strProperty)
                ?? "default string value if strObjProperty is null";

It checks myParent, objProperty and strProperty for null and will assign the default value if any of them is null.

I have extended this feature by creating a extension method that checks for empty too:

string result = (myParent?.objProperty?.strProperty)
                .IfNullOrEmpty("default string value if strObjProperty is null");

Where IfNullOrEmpty is just:

public static string IfNullOrEmpty(this string s, string defaultValue)
{
    return !string.IsNullOrEmpty(s) ?  s : defaultValue);
}
like image 127
Patrick Hofman Avatar answered Nov 14 '22 22:11

Patrick Hofman