DEV Community

Matheus Gomes 👨‍💻
Matheus Gomes 👨‍💻

Posted on

Coalescing operator and Compound assignment operator in C#

Hello! Today i want to ask you this: Have you ever used a Null Coalescing operator or even a Compound assignment operator in C# ?

I never.

Until today i had never heard about this things, so i want to share with you what i learned about and how it can be applied to your code.

The problem

Let's say you want to give a given variable the value of null.

namespace Course {
    class Program {
        static void Main(string[] args) {

            double? x = null;
            double? y = 10.0;

            Console.WriteLine(x.Value);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Now, if we want to print the value on the screen it will accuse the following error:

"Unhandled Exception: System.InvalidOperationException: Nullable object must have a value."
Enter fullscreen mode Exit fullscreen mode

Let's see how to get around this...

The old way 👎

The old and 'commom way' to check this is using if else operators like this:

namespace Course {
    class Program {
        static void Main(string[] args) {

            double? x = null;
            double? y = 10.0;

            // check if x is null
            if (x.HasValue)
                Console.WriteLine(x.Value);
            else
                Console.WriteLine("X is null");

            // check if y is null
            if (y.HasValue)
                Console.WriteLine(y.Value)
            else
                Console.WriteLine("Y is null");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

We see that in this case we cannot place a default value to x and y operators. So we display on screen when it is null.

The Null Coalescing operator way 👌

First, a null coalescing operator (??) is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null, otherwise, it returns the right operand.

So it's mainly used to simplify checking for null values and also assign a default value to a variable when the value is null.

Using our example:

namespace Course {
    class Program {
        static void Main(string[] args) {
            double? x = null;
            double? y = 10.0;

            double x = x ?? 0.0;
            Console.WriteLine(x.Value);
            double y = y ?? 0.0;
            Console.WriteLine(y.Value);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This way i can make a default value on x and y when one of them is null. And so, we can print on screen!

But can it be better?

The Compound assignment operator way 😎

The Compound assignment operator (??=) was introduced on C# 8.0 and has made our job easier. It simply reduces what we have to write and has the same result.

Instead of writing double x = x ?? 0.0;
We can just write double x ??= 0.0;

namespace Course {
    class Program {
        static void Main(string[] args) {
            double? x = null;
            double? y = 10.0;

            double x ??= 0.0;
            double y ??= 0.0;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Simple, right?

Hope you enjoyed this post, it's simple but it's something worth sharing for me.

Thanks for your time!😊

Links:
https://dzone.com/articles/nullable-types-and-null-coalescing-operator-in-c

https://dev.to/mpetrinidev/the-null-coalescing-operator-in-c-8-0-4ib4

https://docs.microsoft.com/pt-br/dotnet/csharp/language-reference/operators/null-coalescing-operator

Top comments (3)

Collapse
 
wakeupmh profile image
Marcos Henrique

🤘

Collapse
 
saint4eva profile image
saint4eva

Good article. Thank you.

Collapse
 
arthurbarbero profile image
Arthur Barbero

Awesome Dude, simple but unknown by most programmers