Share c# 7 switch

tronghungjones

New member
Tuyên bố ## C# 7 Tuyên bố chuyển đổi

C# 7 đã giới thiệu một câu lệnh chuyển đổi mới mạnh mẽ và linh hoạt hơn nhiều so với phiên bản trước.Câu lệnh Switch mới cho phép bạn sử dụng các biểu thức làm giá trị chuyển đổi và nó cũng hỗ trợ nhiều nhãn trường hợp và giảm.

### Cú pháp của câu lệnh chuyển đổi C# 7

Cú pháp của câu lệnh C# 7 Switch như sau:

`` `C#
chuyển đổi (biểu thức)
{
Case Label1:
// các câu lệnh
phá vỡ;
Case Label2:
// các câu lệnh
phá vỡ;
mặc định:
// các câu lệnh
phá vỡ;
}
`` `

Trong đó `Biểu thức` là biểu thức được đánh giá và` Label1`, `Label2`, v.v. là các nhãn trường hợp.

### Sử dụng các biểu thức làm giá trị chuyển đổi

Trong phiên bản trước của C#, giá trị chuyển đổi phải là một hằng số hoặc một biểu thức hằng số thời gian biên dịch.Trong C# 7, bạn có thể sử dụng bất kỳ biểu thức nào làm giá trị chuyển đổi.Điều này cho phép bạn thực hiện những việc như chuyển đổi chiều dài của chuỗi hoặc giá trị của một thuộc tính.

Ví dụ: mã sau sử dụng câu lệnh chuyển đổi để in vào ngày trong tuần cho một ngày nhất định:

`` `C#
DateTime date = new DateTime (2023, 3, 8);

chuyển đổi (ngày.dayofweek)
{
Case Dayofweek.Sunday:
Console.WriteLine ("Chủ nhật");
phá vỡ;
Case Dayofweek.mayday:
Console.WriteLine ("Thứ Hai");
phá vỡ;
trường hợp dayofweek.tuesday:
Console.WriteLine ("Thứ ba");
phá vỡ;
Case Dayofweek.Wednesday:
Console.WriteLine ("Thứ tư");
phá vỡ;
trường hợp dayofweek.thursday:
Console.WriteLine ("Thứ năm");
phá vỡ;
Case Dayofweek.Friday:
Console.WriteLine ("Thứ Sáu");
phá vỡ;
Case Dayofweek.Saturday:
Console.WriteLine ("Thứ bảy");
phá vỡ;
}
`` `

### nhiều nhãn trường hợp và rơi qua

Trong phiên bản trước của C#, nếu một nhãn trường hợp được khớp, các câu lệnh cho trường hợp đó sẽ được thực thi và sau đó câu lệnh Switch sẽ bị chấm dứt.Trong C# 7, bạn có thể có nhiều nhãn trường hợp trên cùng một dòng và các câu lệnh cho tất cả các trường hợp đó sẽ được thực thi.Điều này được gọi là mùa thu.

Ví dụ: mã sau sử dụng câu lệnh chuyển đổi để in vào ngày trong tuần cho một ngày nhất định.Nếu ngày là một ngày trong tuần, mã sẽ in "Ngày trong tuần".Nếu ngày là một ngày cuối tuần, mã sẽ in "Cuối tuần".

`` `C#
DateTime date = new DateTime (2023, 3, 8);

chuyển đổi (ngày.dayofweek)
{
Case Dayofweek.Sunday:
Case Dayofweek.Saturday:
Console.WriteLine ("Cuối tuần");
phá vỡ;
mặc định:
Console.WriteLine ("Ngày trong tuần");
phá vỡ;
}
`` `

### Phần kết luận

Câu lệnh C# 7 Switch là một công cụ mạnh mẽ và linh hoạt, có thể được sử dụng để xử lý nhiều kịch bản khác nhau.Bằng cách sử dụng các biểu thức làm giá trị chuyển đổi, nhiều nhãn trường hợp và giảm, bạn có thể viết mã ngắn gọn và có thể đọc được hơn.

## hashtags

* #C#
* #C #7
* #SwitchStatement
* #Biểu thức
* #MultipleCaselabels
=======================================
Statement ## C# 7 Switch Statement

C# 7 introduced a new switch statement that is much more powerful and flexible than the previous version. The new switch statement allows you to use expressions as the switch value, and it also supports multiple case labels and fall-through.

### Syntax of the C# 7 Switch Statement

The syntax of the C# 7 switch statement is as follows:

```c#
switch (expression)
{
case label1:
// statements
break;
case label2:
// statements
break;
default:
// statements
break;
}
```

where `expression` is the expression to be evaluated, and `label1`, `label2`, etc. are the case labels.

### Using Expressions as the Switch Value

In the previous version of C#, the switch value had to be a constant or a compile-time constant expression. In C# 7, you can use any expression as the switch value. This allows you to do things like switch on the length of a string, or the value of a property.

For example, the following code uses a switch statement to print the day of the week for a given date:

```c#
DateTime date = new DateTime(2023, 3, 8);

switch (date.DayOfWeek)
{
case DayOfWeek.Sunday:
Console.WriteLine("Sunday");
break;
case DayOfWeek.Monday:
Console.WriteLine("Monday");
break;
case DayOfWeek.Tuesday:
Console.WriteLine("Tuesday");
break;
case DayOfWeek.Wednesday:
Console.WriteLine("Wednesday");
break;
case DayOfWeek.Thursday:
Console.WriteLine("Thursday");
break;
case DayOfWeek.Friday:
Console.WriteLine("Friday");
break;
case DayOfWeek.Saturday:
Console.WriteLine("Saturday");
break;
}
```

### Multiple Case Labels and Fall-Through

In the previous version of C#, if a case label was matched, the statements for that case would be executed, and then the switch statement would be terminated. In C# 7, you can have multiple case labels on the same line, and the statements for all of those cases will be executed. This is called fall-through.

For example, the following code uses a switch statement to print the day of the week for a given date. If the date is a weekday, the code will print "Weekday". If the date is a weekend, the code will print "Weekend".

```c#
DateTime date = new DateTime(2023, 3, 8);

switch (date.DayOfWeek)
{
case DayOfWeek.Sunday:
case DayOfWeek.Saturday:
Console.WriteLine("Weekend");
break;
default:
Console.WriteLine("Weekday");
break;
}
```

### Conclusion

The C# 7 switch statement is a powerful and flexible tool that can be used to handle a variety of different scenarios. By using expressions as the switch value, multiple case labels, and fall-through, you can write more concise and readable code.

## Hashtags

* #C#
* #C#7
* #SwitchStatement
* #Expressions
* #MultipleCaselabels
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top