Gupta Ji
Gupta Ji

Reputation:

How to use ternary operator in C#

int number = 5;

How do I write a statement for this in ASP.NET using C#?

Upvotes: 17

Views: 82404

Answers (12)

Alexandr
Alexandr

Reputation: 231

I found this question because I was looking for a syntax for a getter and didn't find a complete answer here. But the answers helped to make the right design. I'm posting it, maybe someone needs it too:
get { return number == 5 ? true : false; }

Upvotes: -2

RPS
RPS

Reputation: 11

using ternary operator statement in c# with Eval. (In aspx page)

<span><%# Eval("productname").ToString().Length<=0 ? "<label style=\"color: red;\">Notes<span></span></label>" : "<label style=\"color: blue;\">Notes<span></span></label>"%></span>

Upvotes: 1

user1773603
user1773603

Reputation:

From @JohnK's comment use:

int five = 5;
string answer = five == 5 ? bool.TrueString : bool.FalseString;

Represents the Boolean value true/false as a string. This field is read-only. https://msdn.microsoft.com/en-us/library/system.boolean.truestring(v=vs.110).aspx

Upvotes: 1

Mahesh
Mahesh

Reputation: 1012

Simplest thing is Console.WriteLine((five == 5).ToString());

Upvotes: 1

Neil
Neil

Reputation: 7437

Yet another variation:

string message = XmlConvert.ToString(5 == five);
Console.Write(message);

Upvotes: 1

dar7yl
dar7yl

Reputation: 3747

Just to be safe, you should put your ternary expressions in parens (), because the ternary operator ?: has subtle precedence which can bite you if you aren't watching.

string answer = ( (five==5) ? ("true") : ("false") );

It's probably not important with this example, but if the ternary is part of a complex expression, precedence rules might make the compiler interpret the expression differently from what you intended.

Upvotes: 2

jrista
jrista

Reputation: 32950

You could keep it really simple. Comparing five to 5 results in a boolean, so the following is also possible:

int five = 5;
Console.WriteLine((five == 5).ToString());

The bool type's ToString() method is already designed to return "True" or "False", and if the lowercase alternative is needed, thats simple too:

int five = 5;
Console.WriteLine((five == 5).ToString().ToLower());

If you don't need it lowercased, you can actually completely eliminate the ToString as well:

int five = 5;
Console.WriteLine(five == 5);

Upvotes: 5

devuxer
devuxer

Reputation: 42354

Response.Write(five == 5 ? "True" : "False");

Though, for this example, I wouldn't use the ternary operator at all:

Response.Write(five == 5);

Upvotes: 2

five==5?console.writeline('true'):console.writeline('false')

It works like this:

<if-expression> ? <code-when-if-expression-evaluates-true> : <code-when-if-expression-evaluates-false>

EDIT:

What I had probably been thkinking:

<%=five==5?'true':'false'%>

Upvotes: 1

Abel
Abel

Reputation: 57149

In ASP.NET, declarative (i.e, where the HTML goes):

<p>Is this five? <%= yourVariable == 5 ? "true" : "false"; %></p>

Or, alternatively, in code behind (i.e., where your C# code and classes are):

someTextBox.Text = yourVariable == 5 ? "true" : "false";

Upvotes: 3

James Cronen
James Cronen

Reputation: 5763

The ternary operator in just about every language works as an inline if statement:

Console.WriteLine((five == 5) ? 'true' : 'false');

(You shouldn't strictly need the inner parens, but I like to include them for clarity.)

If the boolean evaluates to true, then the entire expression is equal to the value between the ? and :. If the boolean evaluates to false, the expression equals the value after the :.

I don't believe you can include lines of code in the middle of the operator. These are simply supposed to be expressions that replace the entire operator "phrase" once the condition is evaluated.

I'm a Java guy and don't really know C#; maybe it's different. But probably not.

Upvotes: 10

roman m
roman m

Reputation: 26521

int five = 5;
string answer = five == 5 ? "true" : "false";

I see that you want to use this to write the values out in ASP.NET, the answer string will hold your desired value, use that as you please.

Upvotes: 37

Related Questions