A C# example, the process of code simplification

The first code:
Insert picture description here
After simplification : After
Insert picture description here
using ternary expressions, one line is done:
Insert picture description here
Ternary expressions in C#:
Ternary expressions generally refer to ternary operators, which are a fixed format in software programming. Be able to make some simple conditional choices. The ternary operator as indicated by the name requires three operands. The syntax is: Conditional expression? Expression 1: Expression 2. Note: The position in front of the question mark is the judgment condition. The judgment result is bool type. When it is true, expression 1 is called, and when it is false, expression 2 is called. The logic is: "If the conditional expression is established or satisfied, execute expression 1, otherwise execute the second." It is often used to set a default value, for example, a certain value does not necessarily exist, then judge whether this value exists, and if it does not exist, give the default value
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44685392/article/details/113483274