【问题标题】:What value should this method be returning? [duplicate]这个方法应该返回什么值? [复制]
【发布时间】:2017-02-20 23:20:44
【问题描述】:

写了这个方法,它一直告诉我不是所有的代码路径都返回一个值。我不知道应该返回什么值。

public int SelectionSort()
{

    reset();
    for (int scan = 1; scan <= work.Length - 1; scan++)
    {
        min = scan;
        max = work[scan];

        for (int i = 0; i < work.Length - scan; i++)
        {
            if (work[i] < max)
            {
                min = work[i];
                max = i;

            }
        }
        Swap(work[min], work[scan]);

    }
}

【问题讨论】:

标签: c# methods error-handling


【解决方案1】:

它不应该返回值,因为没有失败或成功的场景删除只是删除返回类型。但是,您可以让此方法因参数错误而引发异常

public void SelectionSort()
{
    if (work == null )  throw new ArgumentException("work is null");  
    ....
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 2013-10-14
    • 1970-01-01
    相关资源
    最近更新 更多