【问题标题】:C# Method errors: Unreachable code detected and not all code paths return a value”C# 方法错误:检测到无法访问的代码,并非所有代码路径都返回值”
【发布时间】:2023-03-26 00:13:01
【问题描述】:
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. 并非所有代码路径都返回值

我无法弄清楚为什么会发生这种情况......任何帮助将不胜感激。 谢谢!

【问题讨论】:

  • return ptList; 应该在方法的末尾
  • 看看你的return语句在哪里

标签: c#


【解决方案1】:

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

移动返回ptList;在循环之外。

【讨论】:

    【解决方案2】:

    该函数应返回一个 Point3d 对象列表。但是不能保证 for 循环条件为真(i

    您应该将 return 语句移到循环之外,如下所示:

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

    【讨论】:

      【解决方案3】:

      你的函数应该返回List&lt;Point3d&gt;。在循环外返回它

      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;
      

      【讨论】:

        【解决方案4】:

        如果“count”为0,它甚至不会进入第一个。因为 i =0 且 i

        试试这样:

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

        【讨论】:

          【解决方案5】:

          应该是:

          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;   //<------Must be here!
             }
          

          【讨论】:

            【解决方案6】:

            您的代码的问题在于 for 循环。

            它不会达到你的返回值。

            如果你的值小于零。

            例如

            count =0;
            for (int i = 0;i < count;i++) // the loop will end. when count is zero.
            

            与您的内部循环相同:

            count =0;
            for (int j = 0;j < count;j++) // the loop will end. when count is zero.
            

            这就是你遇到错误的原因

            检测到无法访问的代码,并非所有代码路径都返回值

            您可以使用以下代码修复它:

            static Random ran = new Random();
            public static List<Point3d> RandomptGenerator(int num)
            {
              var <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;
            }
            

            【讨论】:

              【解决方案7】:

              如果您想生成num 随机点(请注意,在您当前的代码中创建num * num 点),您所要做的就是

              static class Util
              {
                  static Random ran = new Random();
              
                  public static List<Point3d> RandomptGenerator(int num)
                  {
                    // Create list...
                    List <Point3d> ptList = new List<Point3d>();
              
                    // ...fill it with values...
                    for (int i = 0;i < count; i++) 
                        ptList.Add(new Point3d(ran.Next(0, 40), ran.Next(0, 30), 0.0));
              
                    // ...return the list
                    return ptList;   
                 }
              }
              

              通常,我们使用 Linq 来为我们生成列表:

              using System.Linq
              
              ...
              
              static class Util
              {
                  static Random ran = new Random();
              
                  public static List<Point3d> RandomptGenerator(int num)
                  {
                    return Enumerable
                      .Range(0, num)
                      .Select(_ => new Point3d(ran.Next(0, 40), ran.Next(0, 30), 0.0))
                      .ToList();
                 }
              } 
              

              【讨论】:

                【解决方案8】:

                完美,返回ptList;是在错误的位置...这解决了问题,显然这是有道理的。

                谢谢大家的热心回答

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2023-03-20
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-07-01
                  • 2014-04-16
                  • 2015-02-07
                  • 2012-04-16
                  • 1970-01-01
                  相关资源
                  最近更新 更多