Skip to main content

Command Palette

Search for a command to run...

C# ?? "null coalescing operator"

Published
1 min read

The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null; otherwise, it evaluates the right-hand operand and returns its result. The ?? operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.

string message = "hello world";
string result = message ?? "It was null";
//result == "hello world";

string message = null;
string result = message ?? "It was null";
//result == "It was null";

Thanks to Scott Guthrie

13 views

More from this blog

Fix, Build, Automate

17 posts

Straightforward solutions for modern tech problems