4

Hey I just came across the following statement

return name != null ? name : "NA";

I am just wondering what this is called in .NET

does the ? stand for i.e. then do this... ?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
StevieB
  • 6,263
  • 38
  • 108
  • 193
  • 4
    They are called `ternary operators`. They imply that if the condition is evaluated to `true` then the expression after `?` would be executed otherwise the expression after `:` would be executed. – Lion Mar 14 '12 at 03:30
  • 1
    And this can be shortened to `return name ?? "NA";` – Jonathan Wood Mar 14 '12 at 03:32
  • 1
    Worth pointing out that `??` mentioned by @Jonathan is the `null-coalescing operator`. That is: if the value to the left is null, return the value to the right, so it's appropriate to use instead of the ternary operator. However if you were to do `return string.IsNullOrEmpty(name) ? "NA" : name`, then the null coalescing operator wouldn't be useful there. –  Mar 14 '12 at 03:39
  • This has been asked many times, such as http://stackoverflow.com/questions/3312786/benefits-of-using-the-conditional-ternary-operator, though is basically a 'flaw' of the search as question marks can't be searched (though searching for 'question mark C#' will net you the right question). – Dominic K Mar 14 '12 at 03:41

4 Answers4

11

It's a "conditional operator" commonly known as the Ternary operator

It's found in many programming languages.

sethcall
  • 2,837
  • 1
  • 19
  • 22
2

Just to add to everyone else's answers, note that in...

condition ? trueResult : falseResult

...only condition and either trueResult or falseResult (but not both) will be evaluated. That makes it possible to write code like this...

string name = user == null ? "<nobody>" : user.Name;

...without the risk of a NullReferenceException being thrown since user.Name will only be evaluated if user is non-null. Compare this behavior with VB.NET's If operator and IIf function.

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
  • With current capabilities of the language one could even code `string name = user?.Name ?? ""` – Rob Jun 02 '18 at 18:18
1

As Lion said in the comments, they are called ternary operators, though they are also known as inline if statmenets and conditional operator.

If you want to find out more about them, this Wikipedia page will help, and it has examples for many programming languages: http://en.wikipedia.org/wiki/%3F:

joshhendo
  • 1,964
  • 1
  • 21
  • 28
0

This is the conditional operator which is a ternary operator. Since there are not so many other ternary operators (operator with three arguments) many people believe its called ternary operator, which is imprecise!

wintersolutions
  • 5,173
  • 5
  • 30
  • 51
  • 3
    I refer you to section 7.14 of the C# 4 specification, which states "*The `?:` operator is called the conditional operator. It is at times also called the ternary operator.*" Why do you believe that people who believe that the conditional operator is sometimes called the ternary operator are wrong? – Eric Lippert Mar 14 '12 at 05:35
  • @Eric Lippert Because "ternary operators" are a class of operators not a operator. Thats the same as calling `!` the unary operator. – wintersolutions Mar 14 '12 at 11:47
  • If `!` were the only unary operator in the language, then it would still be clear from context what "the unary operator" was. – Greg D Mar 14 '12 at 12:09
  • @GregD thats true, everybody knows what to think of if you talk about the ternary operator. But that still doesn't make it the name of the conditional operator! – wintersolutions Mar 14 '12 at 13:08
  • The name of the conditional operator is "the conditional operator". The conditional operator is *sometimes also called* "the ternary operator". The name of the President is Barack Obama; he is sometimes called "Mr. President". To say "Barack Obama is also called Mr. President" is a fact, and it is not the statement that his *name* is "Mr. President". – Eric Lippert Mar 14 '12 at 13:32
  • @EricLippert so you finally agree with me that people who say that `?:` is called the ternary operator are wrong? I also support your claim that its a fact that many people do call it ternary operator, as you can read in my orginal answer. – wintersolutions Mar 14 '12 at 13:50
  • @PizzaPill: No. It *is* called the ternary operator. The name of the operator is "the conditional operator". It is frequently also called "the ternary operator", which is not wrong. It's not the best choice; "the conditional operator" is a better choice because (1) it's the actual name of the operator, and (2) it describes what the operator does, rather than how it looks. But it is not *wrong* to say "the ternary operator". I encourage people to make the better choice. – Eric Lippert Mar 14 '12 at 14:36
  • @EricLippert I think we should agree to disagree b/c this discussion is going on for too long. I'd also recommend the one who voted my answer down to read about - lets say - the Python slice operator and then ask himself where he would put that in his worldview. – wintersolutions Mar 14 '12 at 14:45
  • @PizzaPill, the discussion is concerning c#, currently it is indeed the only ternary operator... – ShuggyCoUk Mar 14 '12 at 14:49
  • 4
    Well I think we should agree to **refer to the C# specification as the definitive source**. If the specification needs clarification, I think we should agree that **a member of the C# language design committee is a good person to provide clarification.** Do you disagree with either of those statements? – Eric Lippert Mar 14 '12 at 14:56
  • I think that my answer is imprecise in the context of the C# language, and that the C# specification is imprecise in the context of programming languages/mathematics. – wintersolutions Mar 14 '12 at 15:04
  • @PizzaPill I hope no one ever puts "Type" in a language spec without a full category theoretic basis for it. Heaven forbid! – ShuggyCoUk Mar 14 '12 at 15:12
  • 1
    The C# specification is deliberately "imprecise" in the sense that it does not provide "small step" operational semantics that describe the semantics of the language in terms of mathematical objects. A good comparison is the ECMAScript edition 3 specification, which does so. The C# specification is deliberately a more "big step semantics" specification. The language architects and specification writers wish to keep the specification approachable and readable by end users who do not have degrees in computer science. – Eric Lippert Mar 14 '12 at 15:19
  • I changed my answer from wrong to imprecise, in the hope that that is a phrase everybody can agree on. – wintersolutions Mar 14 '12 at 15:37