Thursday, June 24, 2010

If Statement Using Questionmark (?) and Colon (:)

Here a quick post describing a short notation for if - else statements in C# .Net, which I used for example in the previous post.
A standard if statement assigning a value to a variable will look as follows:
if (Condition)
Variable = "Condition satisfied";
Variable = "Condition not satisfied";
One can shorten this code though using questionmark (?) and colon (:):
Variable = (Condition) ? "Condition satisfied" : "Condition not satisfied";

Here Variable gets assigned "Condition satisfied", if the condition is true, otherwise "Condition not satisfied".

No comments:

Post a Comment