Share c# 8.0 switch

hoangjapanees

New member
Biểu thức ## C# 8.0 Biểu thức chuyển đổi

C# 8.0 đã giới thiệu một cú pháp biểu thức chuyển đổi mới giúp viết các câu lệnh chuyển đổi ngắn gọn và có thể đọc được dễ dàng hơn.Biểu thức chuyển đổi ngắn gọn hơn các câu lệnh chuyển đổi truyền thống và chúng cũng có thể được sử dụng ở những nơi mà các câu lệnh chuyển đổi truyền thống không thể.

### Cú pháp

Cú pháp của biểu thức chuyển đổi tương tự như cú pháp của câu lệnh chuyển đổi truyền thống, nhưng với một vài khác biệt chính.Đầu tiên, biểu thức công tắc sử dụng từ khóa `switch` thay vì câu lệnh` switch`.Thứ hai, biểu thức chuyển đổi sử dụng từ khóa `case` để xác định các trường hợp khác nhau và` khi `từ khóa để xác định các điều kiện cho từng trường hợp.

`` `C#
int value = 5;

// Tuyên bố chuyển đổi truyền thống
chuyển đổi (giá trị) {
trường hợp 1:
Console.WriteLine ("One");
phá vỡ;
Trường hợp 2:
Console.WriteLine ("Hai");
phá vỡ;
Trường hợp 3:
Console.WriteLine ("ba");
phá vỡ;
mặc định:
Console.WriteLine ("Khác");
phá vỡ;
}

// Biểu thức chuyển đổi
int results = switch (value) {
trường hợp 1:
Console.WriteLine ("One");
phá vỡ;
Trường hợp 2:
Console.WriteLine ("Hai");
phá vỡ;
Trường hợp 3:
Console.WriteLine ("ba");
phá vỡ;
mặc định:
Console.WriteLine ("Khác");
phá vỡ;
};
`` `

### Những lợi ích

Có một số lợi ích khi sử dụng biểu thức chuyển đổi trên các câu lệnh chuyển đổi truyền thống.Đầu tiên, các biểu thức chuyển đổi ngắn gọn hơn.Điều này làm cho chúng dễ đọc và hiểu, đặc biệt là khi có nhiều trường hợp.Thứ hai, các biểu thức chuyển đổi có thể được sử dụng ở những nơi mà các câu lệnh chuyển đổi truyền thống không thể.Ví dụ, các biểu thức chuyển đổi có thể được sử dụng trong các biểu thức lambda và cây biểu thức.

### ví dụ

Dưới đây là một số ví dụ về biểu thức chuyển đổi:

* Để in chuỗi "một" nếu giá trị là 1, "hai" nếu giá trị là 2 và "ba" nếu giá trị là 3 và "khác" nếu giá trị là bất cứ điều gì khác:

`` `C#
int value = 5;

int results = switch (value) {
trường hợp 1:
Console.WriteLine ("One");
phá vỡ;
Trường hợp 2:
Console.WriteLine ("Hai");
phá vỡ;
Trường hợp 3:
Console.WriteLine ("ba");
phá vỡ;
mặc định:
Console.WriteLine ("Khác");
phá vỡ;
};
`` `

* Để kiểm tra xem một chuỗi có chứa "ABC" không:

`` `C#
Chuỗi văn bản = "ABCDEF";

bool chứa / chuyển đổi (văn bản) {
Case Text.Contains ("ABC"):
ĐÚNG VẬY;
mặc định:
SAI;
};
`` `

* Để chuyển đổi chuỗi thành số nguyên:

`` `C#
Chuỗi sốTring = "123";

int number = switch (numberTring) {
Case Int.Parse (NumberTring):
số lượng;
mặc định:
Ném arm cái mới mới ("Chuỗi số không hợp lệ");
};
`` `

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

Biểu thức chuyển đổi là một tính năng mới mạnh mẽ trong C# 8.0 có thể giúp viết các câu lệnh chuyển đổi ngắn gọn và có thể đọc được dễ dàng hơn.Chúng có thể được sử dụng trong một loạt các kịch bản và chúng có thể cung cấp một số lợi ích so với các câu lệnh chuyển đổi truyền thống.

## hashtags

* #C#
* #C #8.0
* #SwitchExpressions
* #Ngắn gọn
* #Readable
=======================================
Expressions ## C# 8.0 Switch Expressions

C# 8.0 introduced a new switch expression syntax that makes it easier to write concise and readable switch statements. Switch expressions are more concise than traditional switch statements, and they can also be used in places where traditional switch statements cannot.

### Syntax

The syntax of a switch expression is similar to the syntax of a traditional switch statement, but with a few key differences. First, the switch expression uses the `switch` keyword instead of the `switch` statement. Second, the switch expression uses the `case` keyword to define the different cases, and the `when` keyword to define the conditions for each case.

```c#
int value = 5;

// Traditional switch statement
switch (value) {
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
break;
case 3:
Console.WriteLine("Three");
break;
default:
Console.WriteLine("Other");
break;
}

// Switch expression
int result = switch (value) {
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
break;
case 3:
Console.WriteLine("Three");
break;
default:
Console.WriteLine("Other");
break;
};
```

### Benefits

There are several benefits to using switch expressions over traditional switch statements. First, switch expressions are more concise. This makes them easier to read and understand, especially when there are multiple cases. Second, switch expressions can be used in places where traditional switch statements cannot. For example, switch expressions can be used in lambda expressions and expression trees.

### Examples

Here are some examples of switch expressions:

* To print the string "One" if the value is 1, "Two" if the value is 2, and "Three" if the value is 3, and "Other" if the value is anything else:

```c#
int value = 5;

int result = switch (value) {
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
break;
case 3:
Console.WriteLine("Three");
break;
default:
Console.WriteLine("Other");
break;
};
```

* To check if a string contains the substring "abc":

```c#
string text = "abcdef";

bool containsAbc = switch (text) {
case text.Contains("abc"):
true;
default:
false;
};
```

* To convert a string to an integer:

```c#
string numberString = "123";

int number = switch (numberString) {
case int.Parse(numberString):
numberString;
default:
throw new ArgumentException("Invalid number string");
};
```

### Conclusion

Switch expressions are a powerful new feature in C# 8.0 that can make it easier to write concise and readable switch statements. They can be used in a variety of scenarios, and they can provide a number of benefits over traditional switch statements.

## Hashtags

* #C#
* #C#8.0
* #SwitchExpressions
* #Concise
* #Readable
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top