home / software / tips and tricks / How to use Ternary operator in C# ?

How to use Ternary operator in C# ?

Updated:  10/27/2011 11:10 AM
Author:  Shiju Mathews

Status:    Resolved.


Also represented by - ?:

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. The conditional operator is of the form

Syntax:
condition ? first_expression : second_expression;

Sample code


This can be interpreted as if Y !=0 then s = true
And
if Y ==0 then s = false
Here s will attain value 'true' after execution.
Tags: How to use Ternary operator in C# ?
Updated on: November 2024