简体   繁体   English

C#三元运算符

[英]c# Ternary operator

I am working on a an application that has no documentation (although the code is quite clear and well-written) and I am trying to write some useful technical documentation for the next guy who comes along after I am in a mental hospital. 我正在一个没有文档的应用程序上工作(尽管代码很清楚并且写得很好),并且我正在尝试为下一个在精神病院后出现的人编写一些有用的技术文档。

In the web-service method, if the call to the web-service returns an error, then the Catch code runs to increment the number of retries for the message and set the MessageStatus to "New" (so that it is retried if less than 5) or "Error" (for unknown errors), but there is one line I am not completely sure of and I need to document this process properly: 在Web服务方法中,如果对Web服务的调用返回错误,则运行Catch代码以增加消息的重试次数,并将MessageStatus设置为“ New”(以便在小于此值时重试) 5)或“错误”(针对未知错误),但是我不确定那一行,因此需要正确记录此过程:

catch (Exception ex)
{
int NoRetries = (int)dRow[(int)Common.OutboundSQLFields.Message_Retries];
string messageStatus = (NoRetries < 5) ? Common.MessageStatus(ex) : "Expired";
    ...

Does this mean that if the NoRetries is greater than 4, then the MessageStatus will be set to Expired , else the method Common.MessageStatus will be called to reset the string MessageStatus based on the value of (ex) ? 这是否意味着如果NoRetries大于4,则MessageStatus将被设置为Expired ,否则将调用方法Common.MessageStatus以基于(ex)的值重置字符串MessageStatus

So to make it more self-describing, could I rewrite that Ternary operator code as: 因此,为了使其更加自我描述,我可以将三元运算符代码重写为:

string MessageStatus="";
If (NoRetries > 4)
    {
    MessageStatus = "Expired";
    }
else
    {
    MessageStatus = Common.MessageStatus(ex);
    }

(Statement) ? TRUE : FALSE

So if NoRetries < 5 then Common.MessageStatus(ex) if NoRetries >= 5 then "Expired" 因此,如果NoRetries < 5Common.MessageStatus(ex)如果NoRetries >= 5"Expired"

Hope this clarifies it :) 希望这可以澄清它:)

As the statement is: 声明如下:

string messageStatus = (NoRetries < 5) ? Common.MessageStatus(ex) : "Expired";

It should be like: 应该是这样的:

string MessageStatus="";
if (NoRetries < 5)
    MessageStatus = Common.MessageStatus(ex);
else
    MessageStatus = "Expired";

You reversed it actually. 您实际上已将其撤消。 But your's is correct too. 但是你的也是对的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM