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 attend5days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted Nov 26, 2022, 8:49 AM EST
example :- convert below to ternary
string Name= @(Model.Gender=="Male"?"Mr."+Model.Name:(Model.Gender=="Female" && Model.MaritalStatus=="Unmarried")?"Ms."+Model.Name:"Mrs."+Model.Name);Aravind GovindarajRank #430 · 5.8K reputationPosted Nov 26, 2022, 5:12 PM EST
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 SinghRank #1,609 · 971 reputationPosted Nov 26, 2022, 3:08 PM EST
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 HawaldarRank #110 · 20.2K reputationPosted Nov 26, 2022, 10:39 AM EST
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 ShuklaRank #633 · 3.8K reputationPosted Nov 26, 2022, 9:10 AM EST
https://www.tutorialsteacher.com/csharp/csharp-ternary-operator
Onkar SharmaRank #104 · 20.6K reputationPosted Dec 25, 2022, 7:49 AM EST
Hi Naresh,
To know more about Ternary Operator In C#, visit:
Thanks!
Saravanan GanesanRank #153 · 15.1K reputationPosted Sep 25, 2023, 1:53 PM EDT
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.