How to use ternary operator for this statement in c?

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.

"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.

Bool. TrueString : bool. FalseString; – John K Nov 5 '09 at 5:12.

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.

Just a note: Console.WriteLine() has nothing to do with ASP. NET – roman m Nov 5 '09 at 4:28 agreed, but another note: I like your explanation, well done! :) – Abel Nov 5 '09 at 4:32 @rm: Heh, oops.

Sorry, was copying the other answerer's code and remembered that from the tiny bit of C# I remembered. :-) – Tenner Nov 5 '09 at 4:34 but I forget to say thanks then very very thanks for help me on stack – Gupta Ji Nov 5 '09 at 4:36.

In ASP. NET, declarative (i. E, where the HTML goes): Is this five?

Or, alternatively, in code behind (i.e. , where your C# code and classes are): someTextBox. Text = yourVariable == 5?"true" : "false.

Response. Write(five == 5? "True" : "False"); Though, for this example, I wouldn't use the ternary operator at all: Response.

Write(five == 5).

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).

Five==5?console. Writeline('true'):console. Writeline('false') It works like this:?

: EDIT: What I had probably been thkinking.

Console. WriteLine returns void. The ternary operator requires a return value and must be used as RH expression of an assignment.

– Abel Nov 5 '09 at 4:22 Ah, whoops! I don't have a compiler in front of me to test. Oh well.

– FrustratedWithFormsDesigner Nov 5 '09 at 4:27 are you really read my question I say for asp.net" rel="nofollow">asp.net not for winform and your statement not worked if you do this in asp.net" rel="nofollow">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" – Gupta Ji Nov 5 '09 at 4:27 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 Nov 5 '09 at 4:41.

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.

That's a bit heavy on the parenthesis. – kirk. Burleson Sep 9 '10 at 19:18 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 Sep 9 '10 at 19:54 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 Sep 9 '10 at 19:58.

Yet another variation: string message = XmlConvert. ToString(5 == five); Console. Write(message).

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

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions