Ternary operator in C#
Loading
Ternary operator in C#
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Saravanan GanesanPosted Sep 25, 2023, 5:53 PM
The ternary operator in C# (?:) is a concise conditional expression. It evaluates a condition and returns one of two expressions based on the result. For example,
condition ? trueExpression : falseExpression. If the condition is true, it returnstrueExpression, otherwisefalseExpression. It's a shorthand for if-else statements in some cases.Onkar SharmaPosted Dec 25, 2022, 12:49 PM
Hi Naresh,
To know more about Ternary Operator In C#, visit:
Thanks!
Aravind GovindarajPosted Nov 26, 2022, 10:12 PM
Instead of the conventional If else condition, we go for the ternary operator
conventional :
if ( value == true)
function1()
else
function2()
ternary
value == true ? function1(): function2()
Abhishek SinghPosted Nov 26, 2022, 8:08 PM
You can consider it as the short version of if-else
like you have a variable
bool showValue = true;
now you need to add some conditions based on this number, then you can easily do it with if - else
Ex: if(showValue)
{
Message: "Show the text box value"
}
else
{
Message: "Hide the text box value"
}
With Ternary::
var message = showValue ? "Show the text box value" : "Hide the text box value "
if showValue is true than print "Show the text box value" else "Hide the text box value"
Rajanikant HawaldarPosted Nov 26, 2022, 3:39 PM
https://www.c-sharpcorner.com/blogs/what-is-ternary-operator-in-c-sharp
https://www.c-sharpcorner.com/blogs/ternary-operator-in-c-sharp1
https://www.c-sharpcorner.com/blogs/use-ternary-operator-in-c-sharp-language1
Brahma Prakash ShuklaPosted Nov 26, 2022, 2:10 PM
https://www.tutorialsteacher.com/csharp/csharp-ternary-operator
Sachin SinghPosted Nov 26, 2022, 1:49 PM
example :- convert below to ternary