Question marks and colons in C Sharp (C#)

If you are relatively new to C # and you are looking over some code, you may have encounterd questions marks in your code that you do not know what to make of.

For example:

 string strTime;
 int intHour;
intHour = 12;
strTime = intHour > 11 ? 'After Noon' : 'Morning' ;

The combined use of the question mark and colon above act like an if / else statement.  The correct term to describe this is TERNARY OPERATOR.

What happens is the following. The variable strTime will hold one of the following values “Afternoon” or “Morning”.  How do you know, well it depends on it’s evaluation of the integer variable intHour.  The expression says, if (?) intHour is greater than 11 then display “After Noon” else (:) display “Morning”.

Hope that helps someone.


Leave a comment

Filed under Computer - Technical

Comment on this

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s