views:

265

answers:

6

When you were a kid, did you ever ask your parents how to spell something and they told you to go look it up? My first impression was always, "well if could look it up I wouldnt need help spelling it". (yeah yeah I know phonetics)

...anyway, I was just looking at some code and I found an example like:

 txtbx.CharacterCasing = (checkbox.Checked) ? CharacterCasing.Upper : CharacterCasing.Normal;

I can figure out what this operation does, but obviously, I cant google for ? or : and I cant find them when searching for "c# operators", LINQ, Lambda expressions, etc. So I have to ask this silly question so I can go start reading about it.

What are these operators?

+3  A: 

that is an inline if statement. "?" is the code for the if, ":" is the for the else.

Eclipsed4utoo
It would have been way more easy for begineers if the syntax was an else rather than :!
kenny
@Kenny: "VB's `If...Then...End If` structure would arguably fit your definition, yet C# goes with `{` and `}` for readability and concision.
Adam Robinson
+18  A: 

?: is the conditional operator, and the best way to find out is to ask here!

condition ? first_expression : second_expression;

If condition is true, first expression is evaluated and becomes the result; if false, the second expression is evaluated and becomes the result. Only one of two expressions is ever evaluated.

It's extremely helpful for readability of assignments, when the entire expression is relatively short:

string name = string.IsNullOrEmpty(user.Nickname) ? user.Fullname : user.Nickname

Is much easier and faster than:

string name = user.Fullname;
if(!string.IsNullOrEmpty(user.Nickname))
{
    name = user.Nickname;
}
Rex M
are there any benefits (readability, performance, etc) to using conditional operators
Leroy Jenkins
@DataPimp readability. It's especially helpful with assignment (see my updated answer).
Rex M
Haha, definitely StackOverflow is your best bet! I'd like to add that the ? : operator is a 'ternary' operator because it takes three operands (compare binary operator), and since it happens to be the only ternary operator in a lot of languages, it's also sometimes referred to as *the* ternary operator.
Joren
up one level shows you a list of all the operators which may be helpful for someone not familiar witht he language: http://msdn.microsoft.com/en-us/library/6a71f45d(VS.80).aspx
Dolphin
+1 for the true name.
Michael Myers
Funny thing is, I thought I was ready to get flamed for asking the question, "Whats ? mean" :)
Leroy Jenkins
+5  A: 

? is an inline-if statement. This means that if checkbox.Checked is true, then CharacterCasing.Upper will be the value of the expression, otherwise CharacterCasing.Normal will be.

It works like this:

type value = condition ? trueValue : falseValue;

Adam Robinson
And it is called a ternary operator. The OP can google that to find the answer.
Ed Swangren
Another one that you probably aren't aware of is the coalescing operator (??). http://msdn.microsoft.com/en-us/library/ms173224.aspx
Bryan
This question (and the answers to it) is well worth a read too http://stackoverflow.com/questions/9033/hidden-features-of-c
Bryan
+2  A: 

The ? is also known as the ternary operator

jmein
"Ternary operator" just means "operator that takes three arguments", without specifying their purpose or meaning.
Rex M
@Rex: This is true, but most languages only have one 3-arg operator, hence "THE ternary operator". Even though it isn't specific, this terminology is commonly used to identify the ?: operator.
Steve S
+1  A: 

Incidentally, it so happens that you can search for "?:" on wikipedia and find this.

Note that it's also sometimes called "the" ternary operator, since its the only ternary (3-argument) operator in C-like languages.

Grumdrig
+1  A: 

Btw. As you are learning C# check out ?? operator It is sometimes much better alternative to ?:.

Consider:

Console.WriteLine(user.LastName ?? "no last name provided");

vs:

Console.WriteLine(user.LastName != null ? user.LastName : "no last name provided");
Piotr Czapla
second expression should be: user.LastName != null ? user.LastName : "no last name provided"
Lee
a right, Thanks!
Piotr Czapla