This article is a mirror article of machine translation, please click here to jump to the original article.

View: 3078|Reply: 2

[Source] C# 9.0 New syntax attributes and logic patterns improve the aesthetics of if statements

[Copy link]
Posted on 2/13/2024 4:33:23 PM | | |
The C# language has always been clean and beautiful, and each upgrade brings some syntax sugar that allows us to make our code cleaner. This article shares two examples of tips for using C# 9.0 to enhance the aesthetics of if statements.

Use the property schema instead of IsNullOrEmpty

Whenever you use IsNullOrEmpty, consider replacing it like this:

The property mode is quite flexible, and you can also use it on arrays to make various judgments about arrays. For example, determine whether a string element in an array of nullable strings is empty or blank:

At first, you may feel that the reading experience is not good, but the more you use it, the more you read it, and this concise method is more conducive to reading.

Simplify multiple judgments with logical patterns

For the same value, it can be simplified by comparing it with multiple other values, such as the or and logical patterns:

Many people have wondered why C# 9.0 introduced the or and logical keywords, but this example makes it clear at a glance.

Reference:The hyperlink login is visible.





Previous:MySQL current time plus day and timestamp
Next:Windows creates a file of the specified size
Posted on 2/18/2024 8:31:25 AM |
Take a good look
Posted on 3/4/2024 9:34:10 AM |
Learn the new syntax for summarizing
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com