开发者

Which ternary operator in C# is most popular and mostly used? [closed]

开发者 https://www.devze.com 2022-12-10 17:30 出处:网络
开发者_如何学JAVAIt's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current for
开发者_如何学JAVA It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 13 years ago.

Which ternary operator in C# is most popular and mostly used?


The operator sometimes known as the ternary operator is actually named the conditional operator. It's of the form

A ? B : C

where A is a Boolean expression, and B and C are expressions either of the same type, or of types such that the type of B can be implicitly converted to the type of C or vice versa.

First A is evaluated; if the result is true then B is evaluated to provide the result. Otherwise C is evaluated to provide the result.


It is popular because it leads to shorter and more readable code. Consider this simple example:

int daysInYear = isLeapYear ? 366 : 365;

instead of

if(isLeapYear) {
   daysInYear = 366;
} else {
   daysInYear = 365;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消