c# 中的 iif 等价物

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/822810/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:32:39  来源:igfitidea点击:

iif equivalent in c#

c#.netternary-operatoriif-function

提问by Saif Khan

Is there a IIfequivalent in C#? Or similar shortcut?

中是否有IIf等价物C#?或者类似的捷径?

采纳答案by Joel Coehoorn

C# has the ?ternary operator, like other C-style languages. However, this is not perfectly equivalent to IIf(); there are two important differences.

C# 具有?三元运算符,就像其他 C 风格的语言一样。然而,这并不完全等同于IIf(); 有两个重要的区别。

To explain the first difference, the false-part argument for this IIf()call causes a DivideByZeroException, even though the boolean argument is True.

为了解释第一个区别,此IIf()调用的 false-part 参数导致 a DivideByZeroException,即使布尔参数是True

IIf(true, 1, 1/0)

IIf()is just a function, and like all functions all the arguments must be evaluated before the call is made. Put another way, IIf()does notshort circuit in the traditional sense. On the other hand, this ternary expression does short-circuit, and so is perfectly fine:

IIf()只是一个函数,与所有函数一样,所有参数都必须在调用之前进行评估。换句话说,IIf()确实没有短路传统意义上的。另一方面,这个三元表达式确实短路,所以完全没问题:

(true)?1:1/0;

The other difference is IIf()is not type safe. It accepts and returns arguments of type Object. The ternary operator istype safe. It uses type inference to know what types it's dealing with. Note you can fix this very easily with your own generic IIF(Of T)()implementation, but out of the box that's not the way it is.

另一个区别是IIf()类型不安全。它接受并返回类型为 的参数Object。三元运算符类型安全的。它使用类型推断来了解它正在处理的类型。请注意,您可以使用自己的通用IIF(Of T)()实现非常轻松地解决此问题,但开箱即用的方式并非如此。

If you really want IIf()in C#, you can have it:

如果你真的想要IIf()在 C# 中,你可以拥有它:

object IIf(bool expression, object truePart, object falsePart) 
{return expression?truePart:falsePart;}

or a generic/type-safe implementation:

或通用/类型安全的实现:

T IIf<T>(bool expression, T truePart, T falsePart) 
{return expression?truePart:falsePart;}

On the other hand, if you want the ternary operator in VB, Visual Studio 2008 and later provide a new If()operatorthat works like C#'s ternary operator. It uses type inference to know what it's returning, and it really is an operator rather than a function. This means there's no issues from pre-evaluating expressions, even though it has function semantics.

另一方面,如果您想要 VB 中的三元运算符,Visual Studio 2008 及更高版本提供了一个新的If()运算符,其工作方式类似于 C# 的三元运算符。它使用类型推断来知道它返回的是什么,它实际上是一个运算符而不是一个函数。这意味着预评估表达式没有问题,即使它具有函数语义。

回答by Ris Adams

the ternary operator

三元运算符

bool a = true;

string b = a ? "if_true" : "if_false";

回答by Jon Limjap

It's the ternary operator ?

这是三元运算符 ?

string newString = i == 1 ? "i is one" : "i is not one";

回答by Kevin Pang

VB.NET:

VB.NET:

If(someBool, "true", "false")

C#

C#

someBool ? "true" : "false";

回答by John Gibb

Also useful is the coalesce operator ??:

合并运算符 ?? 也很有用:

VB:

VB:

Return Iif( s IsNot Nothing, s, "My Default Value" )

C#:

C#:

return s ?? "My Default Value";

回答by Zamir

booleanExpression ? trueValue : falseValue;

Example:

例子:

string itemText = count > 1 ? "items" : "item";

http://zamirsblog.blogspot.com/2011/12/c-vb-equivalent-of-iif.html

http://zamirsblog.blogspot.com/2011/12/c-vb-equivalent-of-iif.html

回答by barlop

It's limited in that you can't put statements in there. You can only put values(or things that return/evaluate to values), to return

它的局限性在于您不能在其中放置语句。您只能放置值(或返回/评估值的东西),以返回

This works ('a' is a static int within class Blah)

这有效('a' 是类 Blah 中的静态整数)

Blah.a=Blah.a<5?1:8;

(round brackets are impicitly between the equals and the question mark).

(圆括号隐含在等号和问号之间)。

This doesn't work.

这不起作用。

Blah.a = Blah.a < 4 ? Console.WriteLine("asdf") : Console.WriteLine("34er");
or
Blah.a = Blah.a < 4 ? MessageBox.Show("asdf") : MessageBox.Show("34er");

So you can only use the c# ternary operator for returning values. So it's not quite like a shortened form of an if. Javascript and perhaps some other languages let you put statements in there.

所以你只能使用 c# 三元运算符来返回值。所以它不像是 if 的缩写形式。Javascript 和也许其他一些语言允许您在其中放置语句。