本文介绍了您能帮助简化/修复以下代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的初学者,请你帮忙简化以下代码:



I am a beginner in C#,could you please help simplify following code:

public class AccountInfo
{
               public int AccountId { get; set; }
               public string AccountName { get; set; }
               public bool AccountActive { get; set; }
}

private static bool IsAccountActive(Guid accountId)
{
               if(accountId == null)
               {
                              throw new Exception("Account id is null.");
               }
               else
               {
                              if(AccountIdExists(accountId) == true)
                              {
                                             AccountInfo acctInfo = RetrieveAccountInformation(accountId);
                                             if(acctInfo.AccountActive == true)
                                             {
                                                            return acctInfo.AccountActive
                                             }
                                             else
                                             {
                                                            return acctInfo.AccountActive
                                             }
                              }
                              else
                              {
                                             throw new Exception("Account with account id was not found.");
                              }
               }
}

private static bool AccountIdExists(Guid accountId)
{
}





我尝试了什么:



您能否帮助简化/修复以下代码并解释其中的问题谢谢!



What I have tried:

Can you help simplify/fix following code and explain what is the problem in it.Thank you!

推荐答案

if (AccountIdExists(accountId))
{
    var acctInfo = RetrieveAccountInformation(accountId);
    return acctInfo.AccountActive;
}
else
{
    throw new Exception("Account with account id was not found.");
}


if(acctInfo.AccountActive == true)
{
               return acctInfo.AccountActive // because this line
}
else
{
               return acctInfo.AccountActive // a,d this line are same
}



可以简化为


can be simplified to

return acctInfo.AccountActive


private static bool IsAccountActive(Guid accountId)
  {
    if (accountId == null)
     {
      throw new ArgumentException("Parameter cannot be null", "accountId");
     }

    if (!AccountIdExists(accountId))
     {
       return false;
     }
    AccountInfo acctInfo = RetrieveAccountInformation(accountId);
    return acctInfo.AccountActive;

  }


这篇关于您能帮助简化/修复以下代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 14:30