Conditional Operator को '?:' Symbol द्वारा दर्शाया जाता है। पहला Operand Evaluating Expression होता है। इसमें Right-to-Left Associativity होती है।
Conditional Operator का Syntax:
expression ? expression : expression
Conditional Operator निम्न प्रकार से काम करता है:
पहला Operand Implicitly bool में Convert हो जाता है।
यदि पहला Operand True Evaluate होता है, तो दूसरा Operand Evaluate होता है।
यदि पहला Operand False Evaluate होता है, तो तीसरा Operand Evaluate होता है।
याद रखें, Conditional Expression में अंतिम दो Operands में से केवल एक ही Evaluate होता है।
Example
using System; namespace Demo { class Program { static void Main(string[] args) { int num1 = 100, num2; num2 = (num1 == 100 ? 200 : 0); Console.WriteLine("Number One = " + num1); Console.WriteLine("Number Two = " + num2); Console.ReadKey(); } } }Output
Number One = 100 Number Two = 200