Home > Net >  If the else shorthand, c #
If the else shorthand, c #

Time:09-28

If the else shorthand, c #
Such as:
Int xingbie=0;
String xb=xingbie==0? "Male" : "female";
Such as 2:
String xb=null;
String xbNew=string. IsNullOrEmpty (xb)? "" : xb;
Such as 3:
The StringBuilder sbRet=new StringBuilder ();
SbRet. Append (" \ "explanation " : \ "" + (string. IsNullOrEmpty (drBody [r]." explanation "the ToString (). The Trim ())? "" : drBody [r]." explanation "the ToString (). The Trim ()) +" \ ", ");

CodePudding user response:

This is not short,,, this is called, and the ternary operator,,,

CodePudding user response:

Writing is easy, is also very practical indeed,
To ensure the same type, however, if the else does not have this problem,

CodePudding user response:

The
refer to the original poster weixin_43153506 response:
if the else shorthand, c #
Such as:
Int xingbie=0;
String xb=xingbie==0? "Male" : "female";
Such as 2:
String xb=null;
String xbNew=string. IsNullOrEmpty (xb)? "" : xb;
Such as 3:
The StringBuilder sbRet=new StringBuilder ();
SbRet. Append (" \ "explanation " : \ "" + (string. IsNullOrEmpty (drBody [r]." explanation "the ToString (). The Trim ())? "" : drBody [r]." explanation "the ToString (). The Trim ()) +" \ ", ");

You don't have any questions, just want to express this kind of statement or writing?

CodePudding user response:

The ternary operator can simplify the code, c # has a lot of magical things, slowly to find it

CodePudding user response:

reference 4 floor Summer_djz response:
the ternary operator can simplify the code, c # has a lot of magical things, slowly go find


Few people know c # technology

CodePudding user response:

It is now looking for a instead of be true if the else judgment

CodePudding user response:

refer to the 6th floor not to care, the shackles of solution doesn't open response:
it now be true want to find a replace if the else judge

Write programs like to talk, do you think if you, how there are so many if you want to say

CodePudding user response:

Design pattern point of view, the if statement is bad taste, anyway, all want to eliminate

If it is a special conditional statements, itself has no logic, use? : eliminate the ternary operator
If there is a branch of the business logic blocks, and then they are two different but similar things again, consider using polymorphism to let the system automatically choose the execution

CodePudding user response:

String xb=null;
String xbNew=string. IsNullOrEmpty (xb)? "" : xb;


Can also write
String xb=null;
XbNew=xb?? "";

You have not learned

CodePudding user response:

Mark the operator has been simplified, with the if the else can be more clearly

CodePudding user response:

Good carry of the Microsoft documentation, tutorials need not, what is dishonest practices and "discovered" slowly, Microsoft were giving you translation!
https://docs.microsoft.com/zh-cn/dotnet/csharp/tutorials/intro-to-csharp/
https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/index
In addition, even if what's the magic of c #, also everywhere on the wheel does not have the ternary operator...

CodePudding user response:

refer to the eighth floor HZTLTGG response:
design pattern point of view, the if statement is bad taste, it will eliminate

If it is a special conditional statements, itself has no logic, use? : eliminate the ternary operator
If there is a branch of the business logic blocks, and then they are two different but similar things again, consider using polymorphism to let the system automatically choose to perform the piece of

Consult, polymorphic how to completely replace the if,

CodePudding user response:

Using c # 8 can also wrote:
 string xb=xingbie switch {
0=& gt; "Men,"
1=& gt; "Women,"
_=& gt; "Unknown"
}

CodePudding user response:

On the 14th floor hez2010
reference response:
using c # 8 can also wrote:
 string xb=xingbie switch {
0=& gt; "Men,"
1=& gt; "Women,"
_=& gt; "Unknown"
}

Really good
Long knowledge

CodePudding user response:

C # syntax sugar more and more, in fact, sometimes feel there is no need to, you should be still more flexible in grammar, keep up with the JavaScript? Or do their own characteristics,
  •  Tags:  
  • C#