If 运算符 (Visual Basic)If Operator (Visual Basic)

使用短路计算有条件地返回两个值中的一个值。Uses short-circuit evaluation to conditionally return one of two values. 可以用三个参数或两个参数调用 If 运算符。The If operator can be called with three arguments or with two arguments.

语法Syntax

If( [argument1,] argument2, argument3 )  

如果调用运算符时有三个参数If Operator Called with Three Arguments

使用三个参数调用 If 时,第一个参数的计算结果必须为可强制转换为 Boolean 的值。When If is called by using three arguments, the first argument must evaluate to a value that can be cast as a Boolean. 该 @no__t 值将确定计算并返回其他两个参数中的哪一个。That Boolean value will determine which of the other two arguments is evaluated and returned. 以下列表仅适用于使用三个参数调用 @no__t 0 运算符的情况。The following list applies only when the If operator is called by using three arguments.

部件Parts

术语Term 定义Definition
argument1 必需。Required. BooleanBoolean. 确定要计算和返回的其他参数的值。Determines which of the other arguments to evaluate and return.
argument2 必需。Required. ObjectObject. 如果 argument1 的计算结果为 True,则计算并返回。Evaluated and returned if argument1 evaluates to True.
argument3 必需。Required. ObjectObject. 如果 argument1 的计算结果为 False,则计算并返回; 如果 @no__t 为可以 @no__t 为 null 的可为 null的值,则为null。Evaluated and returned if argument1 evaluates to False or if argument1 is a NullableBoolean variable that evaluates to Nothing.

使用三个参数调用的 @no__t 0 运算符的工作方式类似于 IIf 函数,只不过它使用短路计算。An If operator that is called with three arguments works like an IIf function except that it uses short-circuit evaluation. @No__t-0 函数始终计算其所有三个参数,而具有三个参数的 @no__t 1 运算符只计算两个参数中的两个。An IIf function always evaluates all three of its arguments, whereas an If operator that has three arguments evaluates only two of them. 计算第一个 @no__t 0 参数,并将结果强制转换为 Boolean 值,@no__t 为-2 或 @no__t 为3。The first If argument is evaluated and the result is cast as a Boolean value, True or False. 如果值为 True,则计算 @no__t 并返回其值,但不会计算 argument3If the value is True, argument2 is evaluated and its value is returned, but argument3 is not evaluated. 如果 @no__t @no__t 表达式的值为-1,则计算 argument3 并且返回其值,但不计算 argument2If the value of the Boolean expression is False, argument3 is evaluated and its value is returned, but argument2 is not evaluated. 以下示例说明了使用三个参数时 IfThe following examples illustrate the use of If when three arguments are used:

' This statement prints TruePart, because the first argument is true.
Console.WriteLine(If(True, "TruePart", "FalsePart"))

' This statement prints FalsePart, because the first argument is false.
Console.WriteLine(If(False, "TruePart", "FalsePart"))

Dim number = 3
' With number set to 3, this statement prints Positive.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))

number = -1
' With number set to -1, this statement prints Negative.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))

下面的示例说明了短路计算的值。The following example illustrates the value of short-circuit evaluation. 该示例显示两次尝试将变量 number 除以变量 divisordivisor 为零时除外)。The example shows two attempts to divide variable number by variable divisor except when divisor is zero. 在这种情况下,应返回0,并且不会尝试执行除法,因为将导致运行时错误。In that case, a 0 should be returned, and no attempt should be made to perform the division because a run-time error would result. 由于 @no__t 0 表达式使用短路计算,因此它将计算第二个或第三个参数,具体取决于第一个参数的值。Because the If expression uses short-circuit evaluation, it evaluates either the second or the third argument, depending on the value of the first argument. 如果第一个参数为 true,则除数不为零,并且可以安全地计算第二个参数并执行除法运算。If the first argument is true, the divisor is not zero and it is safe to evaluate the second argument and perform the division. 如果第一个参数为 false,则只计算第三个参数,并返回0。If the first argument is false, only the third argument is evaluated and a 0 is returned. 因此,当除数为0时,将不会尝试执行除法,也不会产生错误结果。Therefore, when the divisor is 0, no attempt is made to perform the division and no error results. 但是,因为 IIf 不使用短路计算,所以即使第一个参数为 false,也会计算第二个参数。However, because IIf does not use short-circuit evaluation, the second argument is evaluated even when the first argument is false. 这将导致运行时被零除错误。This causes a run-time divide-by-zero error.

number = 12

' When the divisor is not 0, both If and IIf return 4.
Dim divisor = 3
Console.WriteLine(If(divisor <> 0, number \ divisor, 0))
Console.WriteLine(IIf(divisor <> 0, number \ divisor, 0))

' When the divisor is 0, IIf causes a run-time error, but If does not.
divisor = 0
Console.WriteLine(If(divisor <> 0, number \ divisor, 0))
' Console.WriteLine(IIf(divisor <> 0, number \ divisor, 0))

如果运算符调用了两个自变量If Operator Called with Two Arguments

可以省略 If 的第一个参数。The first argument to If can be omitted. 这使得仅使用两个参数即可调用运算符。This enables the operator to be called by using only two arguments. 以下列表仅适用于调用带有两个参数的 If 运算符时。The following list applies only when the If operator is called with two arguments.

部件Parts

术语Term 定义Definition
argument2 必需。Required. ObjectObject. 必须是一个引用或可以为 null 的类型。Must be a reference or nullable type. 当计算结果为除 @no__t 0 以外的任何值时,计算并返回该值。Evaluated and returned when it evaluates to anything other than Nothing.
argument3 必需。Required. ObjectObject. 如果 argument2 的计算结果为 Nothing,则计算并返回。Evaluated and returned if argument2 evaluates to Nothing.

当省略 @no__t 0 参数时,第一个参数必须是引用或可以为 null 的类型。When the Boolean argument is omitted, the first argument must be a reference or nullable type. 如果第一个参数的计算结果为 Nothing,则返回第二个参数的值。If the first argument evaluates to Nothing, the value of the second argument is returned. 在所有其他情况下,返回第一个参数的值。In all other cases, the value of the first argument is returned. 下面的示例说明了此计算的工作原理。The following example illustrates how this evaluation works.

' Variable first is a nullable type.
Dim first? As Integer = 3
Dim second As Integer = 6

' Variable first <> Nothing, so its value, 3, is returned.
Console.WriteLine(If(first, second))

second = Nothing
' Variable first <> Nothing, so the value of first is returned again.
Console.WriteLine(If(first, second))

first = Nothing
second = 6
' Variable first = Nothing, so 6 is returned.
Console.WriteLine(If(first, second))

请参阅See also