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

Is there one statement or one line way to do something like this where the string s is declared and the first non-zero value in the expression is assigned?

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

or, more generally,

object obj = Coalesce(obj1, obj2, obj3, ...objx);
+5
source share
2 answers

As Darren Kopp said.

Your expression

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

You can write like this:

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

to put it in other words:

var a = b ?? c;

equivalently

var a = b != null ? b : c;
+14
source

operator .

string a = nullstring ?? "empty!";
+2
source

All Articles