Hi,
Is there substitution for booleans in C#? If bool 1 false then return bool 2 and the opposite? And what to do if there is more then two boolean values - Coalesce, NullIf...?
Thank you very much.
Art.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Jiteendra SampathiraoRank #585 · 4.1K reputationPosted Nov 24, 2010, 1:38 AM EST
function asdasdad()
{
int s=someoperation;
return s;
}
if (s==0)
{
//do something
}
if(s==1)
{
//do something
}
is(s==2)
{
//do something
}
You can assign it to strings also.
If this post helped you, then tick the "Do you like this Answer" checkbox.
Arthur22 reputationPosted Nov 24, 2010, 5:55 AM EST
But is there a single line function/statement in C# that can do the same and be used with boolean types/expressions? I'm looking for smth similar to built-in SQL functions: NVL(string, replace_string) and/or Coalesce(expr1, expr2...... expr n)? Do I have to actually write my own functions for this? And if I have to write my own then how to program a single function/method that would work with numbers, strings and boolean types please? If number then return a number, string-string, bool- return t or f...
If there is a built-in functionality in C# that can do the same please refer me to this and/or give some exact syntax examples please.
Thank you very much.
Art.
B List5 reputationPosted Nov 24, 2010, 7:44 AM EST
int s = (w == null) ? 0 : Convert.ToInt32(w);
If not, then maybe give a brief overview of what you want to use the booleans for.
Arthur22 reputationPosted Nov 24, 2010, 8:42 AM EST
Any more examples would be appreciated.
I'm looking for smth that would work lk this:
if bool1 false then return bool2... else return bool1.
I was hoping there is some kind of single line, no coding required function/method/statement in C# to do this. I can write if but trying to find out if there is an easier way to manipulate with values with minimal coding. I can only give some SQL example:
SELECT coalesce( address1, address2, address3 ) address FROM suppliers;
This single line built-in SQL function is similar to what's below. With minimal coding as above one can do this:
IF address1 is not null THEN
Thank you very much.
Art
B List5 reputationPosted Nov 24, 2010, 9:41 AM EST
IF address1 is not null THEN
result := address1;
ELSIF address2 is not null THEN
result := address2;
ELSIF address3 is not null THEN
result := address3;
ELSE
result := null;
END IF;
could translate to the following in C# (or C++) (I'm going to assume the addresses are of string type and you really mean blank when you indicate "null")
string address1 = "";
string address2 = "";
string address3 = "";
string result;
result = (address1.Length > 0) ? address1 : (address2.Length > 0) ? address2 : (address3.Length > 0) ? address3 : "";
The conditional operator (?) is often used as a shortcut to the if..else statement.
Ex.
if (A == 1) test = "A is 1"
else test = "A is not 1";
is the same as
test = (A == 1) ? "A is 1" : "A is not 1";
Arthur22 reputationPosted Nov 24, 2010, 10:01 AM EST
Art.
Sam HobbsRank #108 · 20.5K reputationPosted Nov 24, 2010, 1:10 PM EST
but as best as I understand it, you can use:
However I think the following will produce the same results: