Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the C# equivalent of the Oracle PL/SQL COALESCE function?

Is there a one statement or one line way to accomplish something like this, where the string s is declared AND assigned the first non-null value in the expression?

//pseudo-codeish
string s = Coalesce(string1, string2, string3);

or, more generally,

object obj = Coalesce(obj1, obj2, obj3, ...objx);
like image 806
COPILOT User Avatar asked Sep 03 '08 19:09

COPILOT User


People also ask

What is C used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language in simple words?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is -= in C?

-= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. C -= A is equivalent to C = C - A.


2 Answers

As Darren Kopp said.

Your statement

object obj = Coalesce(obj1, obj2, obj3, ...objx);

Can be written like this:

object obj = obj1 ?? obj2 ?? obj3 ?? ... objx;

to put it in other words:

var a = b ?? c;

is equivalent to

var a = b != null ? b : c;
like image 167
Erik van Brakel Avatar answered Sep 27 '22 01:09

Erik van Brakel


the ?? operator.

string a = nullstring ?? "empty!";
like image 35
Darren Kopp Avatar answered Sep 24 '22 01:09

Darren Kopp