Interaction.IIf(Boolean, Object, Object) 方法

定義

根據運算式的評估結果,傳回兩個物件當中的其中一個。

C#
public static object? IIf (bool Expression, object? TruePart, object? FalsePart);
C#
public static object IIf (bool Expression, object TruePart, object FalsePart);

參數

Expression
Boolean

必要。 Boolean. 要評估的運算式。

TruePart
Object

必要。 Object. 在 Expression 評估為 True 時傳回。

FalsePart
Object

必要。 Object. 在 Expression 評估為 False 時傳回。

傳回

根據運算式的評估結果,傳回兩個物件當中的其中一個。

範例

這個範例會 IIf 使用 函式來評估 testMe 程序的 參數 checkIt ,如果數量大於 1000,則會傳回 「Large」 這個字,否則會傳回 「Small」 這個字。

VB
Function checkIt(ByVal testMe As Integer) As String
    Return CStr(IIf(testMe > 1000, "Large", "Small"))
End Function

請注意,如果 是 Option StrictOn,您必須使用 CStr 關鍵字明確地將傳回轉換為 ObjectString

備註

IIf 式提供三元 條件運算符的對應專案:? : 在 Visual C++ 中。

適用於

產品 版本
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另請參閱