2

I forgot the syntax of a C# statement and I don't even know the name....

lets say you have a boolean, call it connected, and i want to set some text depending on the connected status. i know there is a simple "one liner" statement but i forgot the syntax and i don't know the name of the type of statement so i can google search it.

here is something close to what i am looking for. the syntax looks something like the following:

string title = ( connected : "[Not Connected]", "[Connected]");

thanks!

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
Jan Tacci
  • 3,131
  • 16
  • 63
  • 83

6 Answers6

10

You are looking for the conditional operator:

string title = connected ? "[Connected]" : "[Not Connected]";
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
5

It's called a Conditional/ternary operator

string title = connected ? "[Not Connected]": "[Connected]";

Note The true condition comes first so in your example it should actually be

string title = connected ? "[Connected]": "[Not Connected]";
Manatherin
  • 4,169
  • 5
  • 36
  • 52
  • conditional operator or ternary operator? – Jan Tacci Aug 13 '12 at 12:49
  • @TackyTacky Ternary simply means "3 operands", it's a conditional operator. – Rudi Visser Aug 13 '12 at 12:50
  • Both. The conditional operator is a ternary operator as it takes three operands. – Daniel Hilgarth Aug 13 '12 at 12:50
  • It's the only operator with three operands, so ternary operator uniquely identifies the operator, but conditional operator describes what it does. Both names are valid. –  Aug 13 '12 at 12:50
  • @DanielHilgarth Was just editting that, had used it from the question example – Manatherin Aug 13 '12 at 12:52
  • 5
    @hvd: It's *currently* the only ternary operator, but that's still a bad reason to use it as a name. That's like calling `protected` "the nine letter access modifier"... – Jon Skeet Aug 13 '12 at 12:52
  • @JonSkeet I do call it the conditional operator, but I don't object if others don't. –  Aug 13 '12 at 12:54
  • 1
    @Manatherin: You said `"[Connected]"` should come first when your first statement can be rewritten as `string title = !connected ? "[Not Connected]": "[Connected]";` – Nikhil Agrawal Aug 13 '12 at 12:58
  • @hvd: It's like arguments vs parameters, and what "pass by reference" means - I think it's worth trying to keep everyone consistent, for clarity. I'm like that though ;) – Jon Skeet Aug 13 '12 at 13:07
  • No objection to that from me :) –  Aug 13 '12 at 13:18
  • @NikhilAgrawal point taken, i was merely trying pointing out that the condition for the true should come first (and keeping it consistent with OP's example) – Manatherin Aug 13 '12 at 14:14
5

I think you mean the conditional operator:

// Sample changed from original code for sanity
string title = connected ? "[Connected]" : "[Not connected]";

Its proper name is the conditional operator, but you'll sometimes here people referring to it as "the ternary operator". That just means it has three operands (vs the unary operators and binary operators) - it doesn't describe what it does at all, and IMO should be avoided. Use its proper name, which describes what it's for: evaluating a condition and then evaluating another expression based on that.

See section 7.14 of the C# 4 spec for details on behaviour etc.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

It sounds like you're talking about the ternary-conditional operator.

string title = condition ? "on true" : "on false";

Note this is not the "ternary operator" as many people might think, ternary simply means to have 3 operands, you can read more on Wikipedia.

In computer science, a ternary operator (sometimes incorrectly called a tertiary operator) is an operator that takes three arguments

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
  • 1
    Under the covers is the compiler really creating code that takes three arguments or does it convert the statement to it's equivalent: if (connected) text = "connected" else text = "not connected"; It seems as if the conditional operator is just a syntactical convenience and doesn't really represent the underlying code. – Jan Tacci Aug 13 '12 at 12:58
  • 1
    @TackyTacky The best way would be to look for yourself, compile a simple application and take a look in the ILDASM to see how it's constructed. Also read 14.13 of the C# Language Spec for more information on the operator. But yes, you're right, this is a convenience operator. – Rudi Visser Aug 13 '12 at 13:00
0

Enter the conditional operator

var title = connected ?  "Connected" : "Not Connected"
Bridge
  • 29,818
  • 9
  • 60
  • 82
Florian Doyon
  • 4,146
  • 1
  • 27
  • 37
0
title= (connected == true) ? "[Connected]" : "[Not Connected]";

or

title= (connected) ? "[Connected]" : "[Not Connected]";
MMK
  • 3,673
  • 1
  • 22
  • 30