c# - 主要: not all code paths return a value

标签 c# list program-entry-point

我正在制作列表并想在控制台中查看它。我有一个错误,它说:

Main(): not all code paths return a value.

也许你可以帮助我?这是我的代码:

namespace ConsoleApplication5
{
    public class DocConfig
    {
        public string Description { get; set; }
        public List<DocPart> Parts { get; set; }
​
        public class DocPart
        {
            public string Title { get; set; }
            public string TexLine { get; set; }

            public class Program
            {
                public static int Main()
                {
                    List<DocPart> Parts = new List<DocPart>();
                    var doc = new DocConfig();
                    doc.Description = "bla bla";
                    doc.Parts = new List<DocPart>();
                    doc.Parts.Add(new DocPart { Title = "aaa", TexLine = @"\include{aaa.tex}" });
                    doc.Parts.Add(new DocPart { Title = "bbb", TexLine = @"\include{bbb.tex}" });
                    foreach (DocPart part in doc.Parts)
                    {
                        Console.WriteLine(part.Title);
                        {
                            Console.ReadLine();
                            return 0;
                        }
                    }
                }
            }
        }
    }
}

最佳答案

Main末尾返回一些内容。像这样:

public static int Main()
{
     List<DocPart> Parts = new List<DocPart>();
     var doc = new DocConfig();
     doc.Description = "bla bla";
     doc.Parts = new List<DocPart>();
     doc.Parts.Add(new DocPart { Title = "aaa", TexLine = @"\include{aaa.tex}" });
     doc.Parts.Add(new DocPart { Title = "bbb", TexLine = @"\include{bbb.tex}" });
     foreach (DocPart part in doc.Parts)
     {
           Console.WriteLine(part.Title);
           {
                 Console.ReadLine();
                 return 0;
           }
     }

     return -1;
}

关于c# - 主要: not all code paths return a value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33689768/

相关文章:

python - 从 Python 列表中获取字符串?

c++ - 处理 main 中的可选参数

C# 将类导入另一个类不起作用

c# - 如何使 SqlContext.Pipe.Send(string str) 立即刷新到 SSMS?

c# - 在 AD 帐户的数据库中存储 SID 或用户名?

list - 列表中的代码片段(克里奥尔语维基)

c++ - 为要在 1 类中使用的结构成员分配常量值

c# - ASP.NET Core Web API 自定义 AuthorizeAttribute 问题

c# - 在我的 C# 应用程序中延迟加载

c# - 动态创建列表 C#