Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How useful is C#'s ?? operator?

Tags:

So I have been intrigued by the ?? operator, but have still been unable to use it. I usually think about it when I am doing something like:

var x = (someObject as someType).someMember; 

If someObject is valid and someMember is null, I could do

var x = (someObject as someType).someMember ?? defaultValue; 

but almost invariably I get into problems when someObject is null, and ?? doesn't help me make this any cleaner than doing the null check myself.

What uses have you guys found for ?? in practical situations?

like image 420
captncraig Avatar asked Nov 06 '09 18:11

captncraig


People also ask

How useful is C language?

By learning C, you will be able to understand and visualise the inner workings of computer systems (like allocation and memory management), their architecture and the overall concepts that drive programming. As a programming language, C also allows you to write more complex and comprehensive programs.

Is it worth learning C in 2021?

There are numerous big tech companies that hire C/C++ developers with some decent salary packages such as Adobe, Oracle, Microsoft, Nvidia, etc. And to learn C/C++ in 2021 is not only beneficial from the career perspectives but it also somehow makes it easier for you to learn other programming languages afterward.

Is learning C worth it in 2022?

C is worth learning in 2022 because it is easy to grasp. It gives you basic knowledge about the inner workings of computer systems and the core concepts that drive programming.

Is C worth learning in 2020?

Yes, you should learn C no matter the year since the language is a good foundation to stand on and will make you a good programmer. That's the quick version of why you should learn C and why it's a good language.


1 Answers

The ?? operator is like the coalesce method in SQL, it gets you the first non-null value.

var result = value1 ?? value2 ?? value3 ?? value4 ?? defaultValue; 
like image 55
Marcel Gosselin Avatar answered Nov 05 '22 13:11

Marcel Gosselin