views:

732

answers:

9
int five = 5;
  • when the variable five is equal to 5, write true
  • when the variable five is not equal to 5, write false

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

+5  A: 
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.

roman m
great answer i try to do this from yesterday 's evening but now my problem is solved
A variation>> string answer = five == 5 ? bool.TrueString : bool.FalseString;
John K
A: 

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'%>

FrustratedWithFormsDesigner
`Console.WriteLine` returns void. The ternary operator requires a return value and must be used as RH expression of an assignment.
Abel
Ah, whoops! I don't have a compiler in front of me to test. Oh well.
FrustratedWithFormsDesigner
are you really read my question i say for asp.net not for winform and your statement not worked if you do this in asp.net you get a error that "Compiler Error Message: CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement"
That error is not related to being WinForms or ASP.NET. The error means what I explained in the first comment here: the ternary operator must be on the right-hand side of an assignment. Oh, and C# needs double quotes :)
Abel
+1  A: 

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";
Abel
+3  A: 

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.

Tenner
just a note: Console.WriteLine() has nothing to do with ASP.NET
roman m
agreed, but another note: I like your explanation, well done! :)
Abel
@rm: Heh, oops. Sorry, was copying the other answerer's code and remembered that from the tiny bit of C# I remembered. :-)
Tenner
but i forget to say thanks then very very thanks for help me on stack
+1  A: 
Response.Write(five == 5 ? "True" : "False");

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

Response.Write(five == 5);
DanM
+1  A: 

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);
jrista
A: 

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.

dar7yl
That's a bit heavy on the parenthesis.
kirk.burleson
The parens force the compiler to interpret the expression as intended, overriding the built-in precedence rules, which are not as intuitive as you figure. This is a simple example, but as the expression becomes more complex, there is more opportunity for the language to misinterpret your intentions.
dar7yl
Also, parens are "free", at least in compiled languages. They can help the reader decipher the expression, especially if you use spacing as code styling.
dar7yl
A: 

Yet another variation:

string message = XmlConvert.ToString(5 == five);
Console.Write(message);
Neil Whitaker
A: 

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

Mahesh