5

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?

Rex M
  • 135,205
  • 29
  • 270
  • 310
Leroy Jenkins
  • 2,438
  • 6
  • 23
  • 30
  • 3
    Consider downloading the C# specification -- the Table of Contents is pretty handy for rapidly determining the names of things. – Eric Lippert Oct 19 '09 at 20:39

6 Answers6

18

?: 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
  • 135,205
  • 29
  • 270
  • 310
  • are there any benefits (readability, performance, etc) to using conditional operators – Leroy Jenkins Oct 19 '09 at 19:16
  • @DataPimp readability. It's especially helpful with assignment (see my updated answer). – Rex M Oct 19 '09 at 19:17
  • 7
    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 Oct 19 '09 at 19:18
  • 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 Oct 19 '09 at 19:18
  • 3
    Funny thing is, I thought I was ready to get flamed for asking the question, "Whats ? mean" :) – Leroy Jenkins Oct 19 '09 at 19:31
5

? 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
  • 171,726
  • 31
  • 271
  • 330
  • And it is called a ternary operator. The OP can google that to find the answer. – Ed S. Oct 19 '09 at 19:18
  • Another one that you probably aren't aware of is the coalescing operator (??). http://msdn.microsoft.com/en-us/library/ms173224.aspx – Bryan Oct 19 '09 at 19:33
  • 1
    This question (and the answers to it) is well worth a read too http://stackoverflow.com/questions/9033/hidden-features-of-c – Bryan Oct 19 '09 at 19:38
3

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

Ryan Alford
  • 7,118
  • 5
  • 38
  • 54
  • It would have been way more easy for begineers if the syntax was an else rather than :! – kenny Oct 19 '09 at 19:15
  • @Kenny: "VB's `If...Then...End If` structure would arguably fit your definition, yet C# goes with `{` and `}` for readability and concision. – Adam Robinson Oct 19 '09 at 19:18
2

The ? is also known as the ternary operator

Josh Mein
  • 26,372
  • 12
  • 72
  • 84
  • 4
    "Ternary operator" just means "operator that takes three arguments", without specifying their purpose or meaning. – Rex M Oct 19 '09 at 19:16
  • @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 Oct 19 '09 at 20:20
1

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
  • 14,990
  • 11
  • 53
  • 68
1

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
  • 23,150
  • 23
  • 90
  • 120
  • second expression should be: user.LastName != null ? user.LastName : "no last name provided" – Lee Oct 19 '09 at 20:30