C#中三元运算符的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1678311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use ternary operator in C#
提问by
int five = 5;
- when the variable five is equal to 5, write true
- when the variable five is not equal to 5, write false
- 当变量 5 等于 5 时,写真
- 当变量5不等于5时,写false
How do I write a statement for this in ASP.NET using C#?
如何使用 C# 在 ASP.NET 中为此编写语句?
采纳答案by roman m
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.
我看到您想使用它在 ASP.NET 中写出值,该answer
字符串将保存您想要的值,请随意使用。
回答by FrustratedWithFormsDesigner
five==5?console.writeline('true'):console.writeline('false')
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'%>
<%=five==5?'true':'false'%>
回答by Abel
In ASP.NET, declarative (i.e, where the HTML goes):
在 ASP.NET 中,声明式(即 HTML 所在的位置):
<p>Is this five? <%= yourVariable == 5 ? "true" : "false"; %></p>
Or, alternatively, in code behind (i.e., where your C# code and classes are):
或者,在后面的代码中(即,您的 C# 代码和类所在的位置):
someTextBox.Text = yourVariable == 5 ? "true" : "false";
回答by James Cronen
The ternary operator in just about every language works as an inline if statement:
几乎所有语言中的三元运算符都用作内联 if 语句:
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 :
.
如果布尔值计算为真,则整个表达式等于?
和之间的值:
。如果布尔值计算为 false,则表达式等于:
.
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.
我是一个 Java 人,并不真正了解 C#;也许它是不同的。但可能不是。
回答by devuxer
Response.Write(five == 5 ? "True" : "False");
Though, for this example, I wouldn't use the ternary operator at all:
不过,对于这个例子,我根本不会使用三元运算符:
Response.Write(five == 5);
回答by jrista
You could keep it really simple. Comparing five to 5 results in a boolean, so the following is also possible:
你可以保持简单。比较 5 到 5 会产生一个布尔值,因此以下也是可能的:
int five = 5;
Console.WriteLine((five == 5).ToString());
The booltype's ToString()method is already designed to return "True" or "False", and if the lowercase alternative is needed, thats simple too:
在布尔类型的的ToString()方法已经被设计为返回“真”或“假”,并且如果需要的小写字母替代,那也简单:
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:
如果您不需要小写,您实际上也可以完全消除 ToString :
int five = 5;
Console.WriteLine(five == 5);
回答by dar7yl
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.
为了安全起见,您应该将三元表达式放在 parens () 中,因为三元运算符 ?: 具有微妙的优先级,如果您不看,它会咬你。
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.
对于本示例,这可能并不重要,但如果三元是复杂表达式的一部分,优先规则可能会使编译器对表达式的解释与您的预期不同。
回答by Neil
Yet another variation:
另一个变体:
string message = XmlConvert.ToString(5 == five);
Console.Write(message);
回答by Mahesh
Simplest thing is Console.WriteLine((five == 5).ToString());
最简单的是 Console.WriteLine((five == 5).ToString());
回答by 5377037
From @JohnK's comment use:
来自@JohnK 的评论使用:
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
将布尔值 true/false 表示为字符串。该字段是只读的。 https://msdn.microsoft.com/en-us/library/system.boolean.truestring(v=vs.110).aspx