C# ?: Operator
The ?: operator, also known as the conditional operator, is utilized to determine the value returned based on the evaluation of a Boolean expression. Its functionality revolves around the concept that if a Condition-Expression holds true, the resulting value is obtained by evaluating and utilizing Expression1. Conversely, if the Condition-Expression is found to be false, the value is determined through the evaluation of Expression2.
This operator presents a viable substitute for the traditional if...else statement, offering a concise and efficient approach to conditionally assigning values. By employing the ?: operator, developers can streamline their code and enhance its readability by encapsulating conditional logic in a single line of code..
Consider the following example :
is same as
Both statements returns same result.Full Source C#