C# 并非所有代码路径都返回值-枚举实践

C# 并非所有代码路径都返回值-枚举实践,c#,enums,return,C#,Enums,Return,我试图执行一个简单的代码来研究枚举主题。 然而,我遇到了这个问题:“并非所有代码路径都返回值”。 代码如下: namespace ConsoleAppTest { class Program { enum Seasons { Winter, Spring, Summer, Fall }; static void Main(string[] args) { WhichSeason(3); }

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

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;
            }
        }
    }
}
名称空间控制台应用程序
{
班级计划
{
列举季节{冬、春、夏、秋};
静态void Main(字符串[]参数)
{
原因(3);
}
季节的静态季节(整数月)
{

如果(月>=1 | |月=4 | |月=7 | |月=10 | | |月您应该处理
其他
情况。您的
整数也可以是
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");
    }
}

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

Edit:我保留了
这一部分的完整性,但似乎您在实现中有一个逻辑错误,应该使用正确的例程

    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;
    }
季节的静态季节(整月)
{

if(month>=1&&month=3&&month=6&&month=9&&month在else if.方法下引入一个else块。该方法在所有情况下都不返回值,应与else一起备份。

您应在逻辑中添加&&month=6&&month=9&&month>运算符,并且还应处理输入与所述条件不匹配的情况-

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;
        }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text.RegularExpressions;
名称空间测试仪
{
班级计划
{
列举季节{冬季、春季、夏季、秋季、NotAValidInput};
公共静态void Main(字符串[]args)
{
Console.WriteLine(WhichSeason(-1));
}
季节的静态季节(整数月)
{
如果(月>=1&&month=4&&month=7&&month=10&&month,则使用开关

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!");
    }
}

如果
month
14
?如果
month==-1
,应该返回什么?我知道,我还没有解决打字错误的问题,只是为了学习,幸运的是,计算机不明白这一点,并且告诉你“如果有人打字错误怎么办?你还没有处理过!”@fubo在许多文化中是的,但其他文化中使用上述代码的约定。当然,在南半球,它被抵消了六个月。非常感谢!我没有意识到为了执行应用程序,必须首先处理不需要的条目。:)@davidgpilot只要方法的返回类型不是
void
(在您的示例
WhichSeason
,返回类型是
Seasons
),通过方法体的所有“路径”都必须
返回值,或
抛出异常(或无限循环)。换句话说,不允许您到达方法体的最后一个结束括号。
}
。请记住,人们将获取返回值,例如
var answer=WhichSeason(3);
Console.WriteLine(WhichSeason(3));
。如果该方法有时只是在没有返回值的情况下退出,则无法工作。我个人更喜欢使用CamelCase:D命名它
    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;
    }
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;
        }
    }

}
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!");
    }
}