SwitchCase 類別

定義

表示 SwitchExpression 的一個案例。

public ref class SwitchCase sealed
public sealed class SwitchCase
type SwitchCase = class
Public NotInheritable Class SwitchCase
繼承
SwitchCase

範例

下列範例示範如何使用 方法來建立代表 switch 語句的運算式,而不使用 SwitchCase 預設案例。

// Add the following directive to the file:
// using System.Linq.Expressions;

// An expression that represents the switch value.
ConstantExpression switchValue = Expression.Constant(2);

// This expression represents a switch statement
// without a default case.
SwitchExpression switchExpr =
    Expression.Switch(
        switchValue,
        new SwitchCase[] {
            Expression.SwitchCase(
                Expression.Call(
                    null,
                    typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                    Expression.Constant("First")
                ),
                Expression.Constant(1)
            ),
            Expression.SwitchCase(
                Expression.Call(
                    null,
                    typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                    Expression.Constant("Second")
                ),
                Expression.Constant(2)
            )
        }
    );

// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda<Action>(switchExpr).Compile()();

// This code example produces the following output:
//
// Second
' Add the following directive to the file:
' Imports System.Linq.Expressions

' An expression that represents the switch value.
Dim switchValue As ConstantExpression = Expression.Constant(2)

' This expression represents a switch statement 
' without a default case.
Dim switchExpr As SwitchExpression =
Expression.Switch(
    switchValue,
    New SwitchCase() {
        Expression.SwitchCase(
            Expression.Call(
                Nothing,
                GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)}),
                Expression.Constant("First")
            ),
            Expression.Constant(1)
        ),
        Expression.SwitchCase(
            Expression.Call(
                Nothing,
                GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)}),
                Expression.Constant("Second")
            ),
            Expression.Constant(2)
        )
    }
)

' The following statement first creates an expression tree,
' then compiles it, and then runs it.
Expression.Lambda(Of Action)(switchExpr).Compile()()

' This code example produces the following output:
'
' Second

屬性

Body

取得這個案例的主體。

TestValues

取得這個案例的值。 當 SwitchValue 符合任何這些值時,會選取這個案例開始執行。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前 ObjectString

Update(IEnumerable<Expression>, Expression)

建立類似這個運算式的新運算式,但使用提供的子系。 如果所有子系都相同,則會傳回這個運算式。

適用於