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

组成部分Parts

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

If使用三个参数调用的运算符的工作方式类似于 IIf 函数,只不过它使用短路计算。An If operator that is called with three arguments works like an IIf function except that it uses short-circuit evaluation. IIf函数始终计算其所有三个参数,而 If 具有三个参数的运算符只计算两个参数中的两个。An IIf function always evaluates all three of its arguments, whereas an If operator that has three arguments evaluates only two of them. 计算第一个 If 参数,并将结果强制转换为 Boolean 值, TrueFalseThe first If argument is evaluated and the result is cast as a Boolean value, True or False. 如果值为 Trueargument2 则计算,并返回其值,但不会对其值进行 argument3 求值。If the value is True, argument2 is evaluated and its value is returned, but argument3 is not evaluated. 如果表达式的值 BooleanFalseargument3 则计算,并返回其值,但不会对其值进行 argument2 求值。If the value of the Boolean expression is False, argument3 is evaluated and its value is returned, but argument2 is not evaluated. 下面的示例演示使用 If 三个参数时的用法:The 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 divisor 除为零时,尝试将变量除以变量 divisorThe 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. 由于 If 表达式使用短路计算,因此它将计算第二个或第三个参数,具体取决于第一个参数的值。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

可以省略的第一个参数 IfThe 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. Object.Object. 必须是引用或可以为 null 的值类型。Must be a reference or nullable value type. 当计算结果为之外的任何值时,计算并返回 NothingEvaluated and returned when it evaluates to anything other than Nothing.
argument3 必需。Required. Object.Object. 如果计算结果为,则计算并返回 argument2 NothingEvaluated and returned if argument2 evaluates to Nothing.

如果 Boolean 省略该参数,则第一个参数必须是引用或可以为 null 的值类型。When the Boolean argument is omitted, the first argument must be a reference or nullable value 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