C# 方法错误 : Unreachable code detected and not all code paths return a value”

标签 c#

static class Util
{
    static Random ran = new Random();

    public static List<Point3d> RandomptGenerator(int num)
    {
        List <Point3d> ptList = new List<Point3d>();
        int count = num;

        for (int i = 0;i < count;i++)
        {
            for (int j = 0;j < count;j++)
            {
                double x = i;
                double y = j;

                x = ran.Next(0, 40);
                y = ran.Next(0, 30);

                ptList.Add(new Point3d(x, y, 0.0));

                return ptList;
            }
        }
    }
}

大家好,

当我尝试编译这段代码时出现以下错误:

  1. 检测到无法访问的代码
  2. 并不是所有的代码路径都有返回值

我不明白为什么会这样...任何帮助将不胜感激。 谢谢!

最佳答案

在您的代码中,您将在 for 循环内返回 ptList。如果 count 为 0 并且您永远不会进入循环,将会发生什么情况。

移动返回点列表;循环外。

关于C# 方法错误 : Unreachable code detected and not all code paths return a value”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45730285/

相关文章:

C# 联合和数组复制

c# - 存档的 APK 文件大小非常巨大 - Xamarin.Android

c# - 您将如何编写钢琴 Octave 音程?

c# - 在 Prism 中处理 PreviewMouseDown 和 PreviewMouseUp 事件

c# - Oracle 同义词错误

c# - 在 PostBack 之后保持 gridview 中 div 的滚动条位置

c# - 比较两个列表的最快方法

c# - 是否有将通用集合公开为只读的良好模式?

c# - Azure 服务结构实例计数

c# - WPF - Databind动态控件类型