【问题标题】:C# Use returned value from Method in IF/ELSE StatementC# 在 IF/ELSE 语句中使用 Method 的返回值
【发布时间】:2017-04-12 07:59:56
【问题描述】:

现在我正在开发一个简单的程序,这是我多次思考的问题。很多时候我运行我的方法两次,因为在运行它们之前检查返回值,我想知道是否有一种方法可以防止这种情况,比如使用我正在检查的方法的返回值。这很难解释,所以这里是我的程序中的一个真实示例。

public class SFDBRepository
{
    public static Domain.SF.SFObject GetSFOrder(string WorkOrd)
    {
        //As you can see here i'm checking on the output of this method, before trying to return it.
        if (Domain.SF.SF.GetOrder(WorkOrd) != null)
        {
            //If the value is not null (My method returns null if no result), return the object
            return Domain.SF.SF.GetOrder(WorkOrd);
        }
        //Same thing happens here. My method runs twice every time almost. 
        else if(Domain.Building_DeliveryPerformance.Building_DeliveryPerformance.GetObject(WorkOrd) != null)
        {
            return Domain.Building_DeliveryPerformance.Building_DeliveryPerformance.GetObject(WorkOrd);
        }
        else
        {
            return null;
        }
    }

}

【问题讨论】:

  • 将返回值捕获到一个变量中,之后使用该变量:var result = Domain.ShopFloor.Shopfloor.GetOrder(WorkOrd) ... if (result != null) return result;
  • 您还应该考虑using directive,这样您就不必在代码中重复使用整个命名空间路径。最好的做法是不要将类命名为与它们所在的命名空间相同。

标签: c# .net if-statement syntax


【解决方案1】:

您可以将其简化为以下代码,它只会调用这些方法一次,并使代码更具可读性:

public class ShopFloorDBRepository
{
    public static Domain.ShopFloor.ShopFloorObject GetShopFloorOrder(string workOrd)
    {
        return Domain.ShopFloor.Shopfloor.GetOrder(workOrd) ??
               Domain.DG9_DeliveryPerformance.DG9_DeliveryPerformance.GetObject(workOrd);
    }
}

解释为什么这有效 - ??运算符(空合并运算符!)基本上说“如果 ?? 左侧的返回值为空,则返回右侧表达式的值”。

这样你只需要调用你的函数一次。

【讨论】:

  • 显然提问者对 c# 来说相对较新,所以简短描述一下你在这里用 ??运算符,以及为什么不需要返回显式 null 也可能会有所帮助。
  • @PaulG 更新了答案以解释空合并运算符的用法
【解决方案2】:
public static Domain.ShopFloor.ShopFloorObject GetShopFloorOrder(string WorkOrd)
{
    //As you can see here i'm checking on the output of this method, before trying to return it.
    Domain.ShopFloor.ShopFloorObject wo = Domain.ShopFloor.Shopfloor.GetOrder(WorkOrd);
    if (wo != null)
    {
        //If the value is not null (My method returns null if no result), return the object
        return wo;
    }
    //Same thing happens here. My method runs twice every time almost. 
    Domain.ShopFloor.ShopFloorObject yowo = Domain.DG9_DeliveryPerformance.DG9_DeliveryPerformance.GetObject(WorkOrd);
    if(yowo != null)
    {
        return yowo;
    }

    /* default return */
    return null;

}

PS

你有点在做“工厂模式”

http://www.dofactory.com/net/factory-method-design-pattern

【讨论】:

  • 这段代码甚至无法编译,因为你在“else if”语句之前放置了一行代码,但在第一个“if”分支主体的大括号之外。 .
  • 最后两个子句是多余的:如果它不为null,则返回它,如果为null,则返回null。不管它是否无效,这与简单地“退货”有什么不同?
  • 因为第一个测试返回如果为真,原始代码和这个都不需要else if...正如@EricLippert 指出的那样,整个后半部分可以替换为return Domain.DG9_DeliveryPerformance.DG9_DeliveryPerformance.GetObject(WorkOrd);
  • 我修复了代码。我试图推动 OP 需要的想法。我认为他/她得到了。
  • 这段代码不是最小的代码。请参阅@JamesHarcourt。但是,恕我直言,这是最易读和调试友好的版本。
【解决方案3】:

在我看来,您可能正在使用一个临时变量来保存结果,您可以对其进行测试并返回。

public class ShopFloorDBRepository
{
  public static Domain.ShopFloor.ShopFloorObject GetShopFloorOrder(string WorkOrd)
  {
    var result = Domain.ShopFloor.GetOrder(WorkOrd);

    if (result != null) return result;
    ...

这是一种常见的范例,尤其是当被调用的方法很昂贵和/或具有您不希望发生两次的副作用时。

这里,“var”声明将“result”的类型设置为被调用方法返回的类型;您也可以使用实际类型的名称。

如果您希望像这样进行两种不同类型的测试,则需要两个不同的变量,除非它们具有相同的类型(在这种情况下,它们看起来确实如此)。

需要完整类型的替代机制,您还将看到:

public static ShopFloorObject GetShopFloorOrder(string WorkOrd)
{
    ShopFloorObject result;

    if ( (result = Domain.ShopFloor.GetOrder(WorkOrd)) != null )
        return result;
    if ( (result = Domain.DG9_DeliveryPerformance.DG9_DeliveryPerformance.GetObject(WorkOrd)) != null)
        return result;
    return null;

在这里,您明确声明返回值的类型,然后进行您指定的两个调用,针对 null 测试结果,并返回第一个非 null 值。

【讨论】:

    猜你喜欢
    • 2020-08-20
    • 2014-12-19
    • 2021-08-31
    • 1970-01-01
    • 2020-07-12
    • 2021-08-19
    • 2023-02-21
    • 2014-01-29
    • 1970-01-01
    相关资源
    最近更新 更多