【问题标题】:Not all code paths return a value - Enum practice并非所有代码路径都返回一个值 - 枚举实践
【发布时间】:2017-09-22 09:42:33
【问题描述】:

我尝试执行一个简单的代码来研究枚举主题。 然而,我遇到了这个问题:“并非所有代码路径都返回一个值”。 代码如下:

namespace ConsoleAppTest
{
    class Program
    {
        enum Seasons { Winter, Spring, Summer, Fall };

        static void Main(string[] args)
        {
            WhichSeason(3);
        }

        static Seasons WhichSeason(int month)
        {
            if (month >= 1 || month <= 3)
            {
                return Seasons.Winter;
            }
            else if (month >= 4 || month <= 6)
            {
                return Seasons.Spring;
            }
            else if (month >= 7 || month <= 9)
            {
                return Seasons.Summer;
            }
            else if (month >= 10 || month <= 12)
            {
                return Seasons.Fall;
            }
        }
    }
}

我想知道是什么导致了这个问题。 谢谢:)

【问题讨论】:

  • 如果month14 怎么办?
  • 如果month == -1应该返回什么?
  • 我知道,我还没解决拼写错误,只是为了学习枚举
  • 不幸的是,计算机不理解这一点,并告诉你“如果有人打错了怎么办?你还没有处理!”
  • @fubo 在许多文化中,是的,但其他文化使用上述代码的约定。当然,在南半球,它被抵消了六个月。

标签: c# enums return


【解决方案1】:

您应该处理else 案例。您的month 整数也可以是&lt;1&gt;12

static Seasons WhichSeason(int month)
{
    if (month >= 1 && month <= 3)
    {
        return Seasons.Winter;
    }
    else if (month >= 4 && month <= 6)
    {
        return Seasons.Spring;
    }
    else if (month >= 7 && month <= 9)
    {
        return Seasons.Summer;
    }
    else if (month >= 10 && month <= 12)
    {
        return Seasons.Fall;
    }
    else
    {
        throw new ArgumentOutOfRangeException("invalid month");
    }
}

所以如果你打电话

WhichSeason(13); //throws exception

【讨论】:

  • 非常感谢!我没有意识到必须首先处理不需要的条目才能执行应用程序。 :)
  • @davidgpilot 只要方法的返回类型是void 以外的任何other(在您的示例中为WhichSeason,返回类型为Seasons),通过方法体的所有“路径”必须return 一个值,或throw 一个异常(或无限循环)。换句话说,你不能到达方法体的最后一个大括号}。请记住,人们会选择返回值,例如var answer = WhichSeason(3);Console.WriteLine(WhichSeason(3));。如果该方法有时会退出而没有返回值,那将无法正常工作。
【解决方案2】:

如果month,比如-1123,应该返回什么?您可以通过两种主要方式解决问题,静默

    // Please, notice "None"
    enum Seasons { None, Winter, Spring, Summer, Fall };

    static void Main(string[] args)
    {
        WhichSeason(3);
    }

    static Seasons WhichSeason(int month)
    {
        if (month >= 1 && month <= 3)
            return Seasons.Winter;
        else if (month >= 4 && month <= 6)
            return Seasons.Spring;
        else if (month >= 7 && month <= 9)
            return Seasons.Summer;
        else if (month >= 10 && month <= 12)
            return Seasons.Fall;
        else
            return Seasons.None;
    }

或者抛出适当的异常ArgumentOutOfRangeException的情况下

    enum Seasons {  Winter, Spring, Summer, Fall };

    static void Main(string[] args)
    {
        WhichSeason(3);
    }

    static Seasons WhichSeason(int month)
    {
        if (month >= 1 && month <= 3)
            return Seasons.Winter;
        else if (month >= 4 && month <= 6)
            return Seasons.Spring;
        else if (month >= 7 && month <= 9)
            return Seasons.Summer;
        else if (month >= 10 && month <= 12)
            return Seasons.Fall;
        else
            throw new ArgumentOutOfRangeException(
              "month", 
              "month must be in [1..12] range."); // Exception
    }

编辑:我已经保留了WhichSeason完整,但是你在实现中似乎有一个逻辑错误并且正确例程应该是

    static Seasons WhichSeason(int month)
    {
        if (month >= 1 && month <= 2 || month == 12) // Jan, Feb and Dec 
            return Seasons.Winter;
        else if (month >= 3 && month <= 5) // Mar-May
            return Seasons.Spring;
        else if (month >= 6 && month <= 8) // Jun-Aug
            return Seasons.Summer;
        else if (month >= 9 && month <= 11) // Sep-Nov
            return Seasons.Fall;
        else
            return Seasons.None;
    }

【讨论】:

  • 我个人更喜欢用 CamelCase 命名它:D
【解决方案3】:

您应该在逻辑中添加 && 运算符,并且还应该在输入与您描述的条件不匹配时进行处理 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{

     class Program
    {
        enum Seasons { Winter, Spring, Summer, Fall,NotAValidInput };

       public static void Main(string[] args)
        {
           Console.WriteLine(WhichSeason(-1));
        }

        static Seasons WhichSeason(int month)
        {
            if (month >= 1 && month <= 3)
            {
                return Seasons.Winter;
            }
            else if (month >= 4 && month <= 6)
            {
                return Seasons.Spring;
            }
            else if (month >= 7 && month <= 9)
            {
                return Seasons.Summer;
            }
            else if (month >= 10 && month <= 12)
            {
                return Seasons.Fall;
            }
            return Seasons.NotAValidInput;
        }
    }

}

【讨论】:

    【解决方案4】:

    在 else if 下引入一个 else 块。方法并非在所有情况下都返回值,应使用 else 备份。

    【讨论】:

      【解决方案5】:

      带开关

      static Seasons WhichSeason(int month)
      {
          switch (month)
          {           
              case 1:
              case 2:
              case 3:
                  return Seasons.Spring;
              case 4:
              case 5:
              case 6:
                  return Seasons.Summer;
              case 7:
              case 8:
              case 9:
                  return Seasons.Fall;
              case 10:
              case 11:
              case 12:
                  return Seasons.Winter;
              default:
                  throw new Exception("The month is invalid!");
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-14
        • 1970-01-01
        • 1970-01-01
        • 2011-12-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多