Expression.Switch Yöntem

Tanım

Deyimi SwitchExpression temsil eden bir switch oluşturur.

Aşırı Yüklemeler

Switch(Type, Expression, Expression, MethodInfo, SwitchCase[])

SwitchExpression Varsayılan büyük/küçük harf içeren bir switch deyimi temsil eden bir oluşturur.

Switch(Expression, SwitchCase[])

SwitchExpression Varsayılan büyük/küçük harf olmadan bir switch deyimi temsil eden bir oluşturur.

Switch(Expression, Expression, SwitchCase[])

SwitchExpression Varsayılan büyük/küçük harf içeren bir switch deyimi temsil eden bir oluşturur.

Switch(Expression, Expression, MethodInfo, IEnumerable<SwitchCase>)

SwitchExpression Varsayılan büyük/küçük harf içeren bir switch deyimi temsil eden bir oluşturur.

Switch(Expression, Expression, MethodInfo, SwitchCase[])

SwitchExpression Varsayılan büyük/küçük harf içeren bir switch deyimi temsil eden bir oluşturur.

Switch(Type, Expression, Expression, MethodInfo, IEnumerable<SwitchCase>)

SwitchExpression Varsayılan büyük/küçük harf içeren bir switch deyimi temsil eden bir oluşturur.

Switch(Type, Expression, Expression, MethodInfo, SwitchCase[])

Kaynak:
SwitchExpression.cs
Kaynak:
SwitchExpression.cs
Kaynak:
SwitchExpression.cs

SwitchExpression Varsayılan büyük/küçük harf içeren bir switch deyimi temsil eden bir oluşturur.

C#
public static System.Linq.Expressions.SwitchExpression Switch (Type type, System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression defaultBody, System.Reflection.MethodInfo comparison, params System.Linq.Expressions.SwitchCase[] cases);
C#
public static System.Linq.Expressions.SwitchExpression Switch (Type? type, System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression? defaultBody, System.Reflection.MethodInfo? comparison, params System.Linq.Expressions.SwitchCase[]? cases);

Parametreler

type
Type

Anahtarın sonuç türü.

switchValue
Expression

Her durumda test edilecek değer.

defaultBody
Expression

Herhangi bir servis talebiyle eşleşmiyorsa anahtarın switchValue sonucu.

comparison
MethodInfo

Kullanılacak eşitlik karşılaştırma yöntemi.

cases
SwitchCase[]

Bu anahtar ifadesi için servis talebi kümesi.

Döndürülenler

Oluşturulan SwitchExpression.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Switch(Expression, SwitchCase[])

Kaynak:
SwitchExpression.cs
Kaynak:
SwitchExpression.cs
Kaynak:
SwitchExpression.cs

SwitchExpression Varsayılan büyük/küçük harf olmadan bir switch deyimi temsil eden bir oluşturur.

C#
public static System.Linq.Expressions.SwitchExpression Switch (System.Linq.Expressions.Expression switchValue, params System.Linq.Expressions.SwitchCase[] cases);
C#
public static System.Linq.Expressions.SwitchExpression Switch (System.Linq.Expressions.Expression switchValue, params System.Linq.Expressions.SwitchCase[]? cases);

Parametreler

switchValue
Expression

Her durumda test edilecek değer.

cases
SwitchCase[]

Bu anahtar ifadesi için servis talebi kümesi.

Döndürülenler

Oluşturulan SwitchExpression.

Örnekler

Aşağıdaki örnekte, varsayılan durum olmadan switch deyimini temsil eden bir ifadenin nasıl oluşturulacağı gösterilmektedir.

C#
// 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

Açıklamalar

türüne sahip olmadığı sürece nesnedeki SwitchExpression tüm SwitchCase nesneler aynı türe SwitchExpressionvoidsahip olmalıdır.

Her SwitchCase nesnenin örtük break bir deyimi vardır, yani bir büyük/küçük harf etiketinden diğerine örtük bir düşüş yoktur.

Herhangi switchValue bir durumla eşleşmiyorsa, özel durum oluşturmaz.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Switch(Expression, Expression, SwitchCase[])

Kaynak:
SwitchExpression.cs
Kaynak:
SwitchExpression.cs
Kaynak:
SwitchExpression.cs

SwitchExpression Varsayılan büyük/küçük harf içeren bir switch deyimi temsil eden bir oluşturur.

C#
public static System.Linq.Expressions.SwitchExpression Switch (System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression defaultBody, params System.Linq.Expressions.SwitchCase[] cases);
C#
public static System.Linq.Expressions.SwitchExpression Switch (System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression? defaultBody, params System.Linq.Expressions.SwitchCase[]? cases);

Parametreler

switchValue
Expression

Her durumda test edilecek değer.

defaultBody
Expression

Herhangi bir servis talebiyle eşleşmiyorsa anahtarın switchValue sonucu.

cases
SwitchCase[]

Bu anahtar ifadesi için servis talebi kümesi.

Döndürülenler

Oluşturulan SwitchExpression.

Örnekler

Aşağıdaki örnekte, varsayılan büyük/küçük harf içeren bir switch deyimini temsil eden bir ifadenin nasıl oluşturulacağı gösterilmektedir.

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

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

// This expression represents a switch statement
// that has a default case.
SwitchExpression switchExpr =
    Expression.Switch(
        switchValue,
        Expression.Call(
                    null,
                    typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                    Expression.Constant("Default")
                ),
        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:
//
// Default

Açıklamalar

türüne sahip olmadığı sürece nesnedeki SwitchExpression tüm SwitchCase nesneler aynı türe SwitchExpressionvoidsahip olmalıdır.

Her SwitchCase nesnenin örtük break bir deyimi vardır, yani bir büyük/küçük harf etiketinden diğerine örtük bir düşüş yoktur.

Hiçbir switchValue servis talebiyle eşleşmiyorsa, tarafından defaultBody temsil edilen varsayılan durum çalıştırılır.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Switch(Expression, Expression, MethodInfo, IEnumerable<SwitchCase>)

Kaynak:
SwitchExpression.cs
Kaynak:
SwitchExpression.cs
Kaynak:
SwitchExpression.cs

SwitchExpression Varsayılan büyük/küçük harf içeren bir switch deyimi temsil eden bir oluşturur.

C#
public static System.Linq.Expressions.SwitchExpression Switch (System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression defaultBody, System.Reflection.MethodInfo comparison, System.Collections.Generic.IEnumerable<System.Linq.Expressions.SwitchCase> cases);
C#
public static System.Linq.Expressions.SwitchExpression Switch (System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression? defaultBody, System.Reflection.MethodInfo? comparison, System.Collections.Generic.IEnumerable<System.Linq.Expressions.SwitchCase>? cases);

Parametreler

switchValue
Expression

Her durumda test edilecek değer.

defaultBody
Expression

Herhangi bir servis talebiyle eşleşmiyorsa anahtarın switchValue sonucu.

comparison
MethodInfo

Kullanılacak eşitlik karşılaştırma yöntemi.

cases
IEnumerable<SwitchCase>

Bu anahtar ifadesi için servis talebi kümesi.

Döndürülenler

Oluşturulan SwitchExpression.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Switch(Expression, Expression, MethodInfo, SwitchCase[])

Kaynak:
SwitchExpression.cs
Kaynak:
SwitchExpression.cs
Kaynak:
SwitchExpression.cs

SwitchExpression Varsayılan büyük/küçük harf içeren bir switch deyimi temsil eden bir oluşturur.

C#
public static System.Linq.Expressions.SwitchExpression Switch (System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression defaultBody, System.Reflection.MethodInfo comparison, params System.Linq.Expressions.SwitchCase[] cases);
C#
public static System.Linq.Expressions.SwitchExpression Switch (System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression? defaultBody, System.Reflection.MethodInfo? comparison, params System.Linq.Expressions.SwitchCase[]? cases);

Parametreler

switchValue
Expression

Her durumda test edilecek değer.

defaultBody
Expression

Herhangi bir servis talebiyle eşleşmiyorsa anahtarın switchValue sonucu.

comparison
MethodInfo

Kullanılacak eşitlik karşılaştırma yöntemi.

cases
SwitchCase[]

Bu anahtar ifadesi için servis talebi kümesi.

Döndürülenler

Oluşturulan SwitchExpression.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Switch(Type, Expression, Expression, MethodInfo, IEnumerable<SwitchCase>)

Kaynak:
SwitchExpression.cs
Kaynak:
SwitchExpression.cs
Kaynak:
SwitchExpression.cs

SwitchExpression Varsayılan büyük/küçük harf içeren bir switch deyimi temsil eden bir oluşturur.

C#
public static System.Linq.Expressions.SwitchExpression Switch (Type type, System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression defaultBody, System.Reflection.MethodInfo comparison, System.Collections.Generic.IEnumerable<System.Linq.Expressions.SwitchCase> cases);
C#
public static System.Linq.Expressions.SwitchExpression Switch (Type? type, System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression? defaultBody, System.Reflection.MethodInfo? comparison, System.Collections.Generic.IEnumerable<System.Linq.Expressions.SwitchCase>? cases);

Parametreler

type
Type

Anahtarın sonuç türü.

switchValue
Expression

Her durumda test edilecek değer.

defaultBody
Expression

Herhangi bir servis talebiyle eşleşmiyorsa anahtarın switchValue sonucu.

comparison
MethodInfo

Kullanılacak eşitlik karşılaştırma yöntemi.

cases
IEnumerable<SwitchCase>

Bu anahtar ifadesi için servis talebi kümesi.

Döndürülenler

Oluşturulan SwitchExpression.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0