Thursday, May 6, 2010

How to write conditional statement without using if key word in c# or How to use ternary operator?

The below code is a replacement of if keyword in c#  which you can accomplish by using the the conditional operator (?:)

condition ? first_expression : second_expression;

Ifyou excecute the below code it will print the value 20 .

int i = (2 > 3) ? 10 : 20;
Response.Write(i.ToString());

If we split the above code then (2 > 3) is considered as the condition, the result before : will be returned if the condition is true and the result after the : will be returned if it is false.

More Information can be out in the link below
http://msdn.microsoft.com/en-us/library/ty67wk28(VS.80).aspx

No comments:

Post a Comment