C#-如何在if语句中使用作废的方法

本文关键字:方法 if 语句 C#- | 更新日期: 2023-09-27 18:08:29

我正在编写一个程序,向用户发送一封包含信息的电子邮件。我试图实现的是,如果电子邮件发送成功,那么它将写入电子邮件发送成功的日志文件。我唯一的问题是,我发现很难让if语句做到这一点。我知道我不能把这个方法放入if语句中,因为你不能把一个作废的方法转换成布尔值,但我还能用什么其他方法呢?下面是我在SendEmail方法中尝试的代码。

SendEmail(BuildEmailBody(transaction, myHomeInformation),subjectLine);
    if (SendEmail.Success())
     {
       BOAssistant.WriteLine                 
     }

这是一种方法:

private static void SendEmail(string emailBody, string emailSubject)
     {
        //This is the method that will create the email for you
        Email email = new Email();
        email.To.Add("POvermyer@TandT.com");
        email.Subject = emailSubject;
        email.Body = emailBody;
        email.Send();
    }

C#-如何在if语句中使用作废的方法

如果你知道一封不成功的电子邮件会引发异常,你可以这样处理:

try
{
    SendEmail(BuildEmailBody(transaction, myHomeInformation),subjectLine);
    BOAssistant.WriteLine  //log success here
}
catch(Exception ex)
{
    //unsuccessful here
}    
if (SendEmail(BuildEmailBody(transaction, myHomeInformation),subjectLine))
{
    BOAssistant.WriteLine                 
}

private static bool SendEmail(string emailBody, string emailSubject)
 {
    //This is the method that will create the email for you
    Email email = new Email();
    email.To.Add("POvermyer@TandT.com");
    email.Subject = emailSubject;
    email.Body = emailBody;
    try{
    email.Send();
    } catch(Exception e) { return false; }
    return true;
}

bool isSent;
SendEmail(BuildEmailBody(transaction, myHomeInformation),subjectLine, out isSent);
if (isSent)
{
    BOAssistant.WriteLine                 
}

private static void SendEmail(string emailBody, string emailSubject, out bool isSent)
 {
    //This is the method that will create the email for you
    Email email = new Email();
    email.To.Add("POvermyer@TandT.com");
    email.Subject = emailSubject;
    email.Body = emailBody;
    try{
    email.Send();
    } catch(Exception e) { isSent = false; }
    isSent = true;
}

SendEmail(BuildEmailBody(transaction, myHomeInformation),subjectLine, () => BOAssistant.WriteLine);

private static void SendEmail(string emailBody, string emailSubject, Action onSuccess)
 {
    //This is the method that will create the email for you
    Email email = new Email();
    email.To.Add("POvermyer@TandT.com");
    email.Subject = emailSubject;
    email.Body = emailBody;
    try{
    email.Send();
    } catch(Exception e) { return; }
    onSuccess();
}

如果你的方法必须是void返回类型(这是我根据问题标题假设的(,那么你可以引入一个out参数。在方法调用之前不必对其进行初始化,但必须在方法本身内部为其赋值。out参数是通过引用传递的,因此被调用者为其分配的任何值都会反映在调用者中。

bool sentSuccessfully;
SendEmail(string emailBody, string emailSubject, out sentSuccessfully)
if(sentSuccessfully)
{
    // do whatever you need
}

当然,您需要修改SendEmail方法以包含此附加参数。

最好的选择是将sendmail方法的返回类型更改为bool,并在if语句中使用它。

private static bool SendEmail(string emailBody, string emailSubject)
 {
   try
   {
    Email email = new Email();
    email.To.Add("POvermyer@TandT.com");
    email.Subject = emailSubject;
    email.Body = emailBody;
    email.Send();
    return true;
   }
   catch(Exception ex)
   {
    return false; 
   }
}

如果改变方法返回类型不是一个选项,那么你可以这样做,

bool IsSuccessfullySent;
try
{
 SendEmail(BuildEmailBody(transaction, myHomeInformation),subjectLine);
 IsSuccessfullySent= true;
}
catch(Exception ex)
{
 IsSuccessfullySent=false;
}
if (IsSuccessfullySent)
 {
   BOAssistant.WriteLine                 
 }