Main: not all code paths return a value

2020-01-25 12:19发布

问题:

I am making list and want to look at it in console. I have one error, It says:

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

Maybe you could help me? Here is my code:

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

回答1:

Return something at the end of Main. Like this:

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


回答2:

The reason that you are getting this error is because when you declared the method

public static int Main()

you declared it would return an int value. However, you only return that value in a place which might never be run. For instance, it would never be run if the doc.Parts list were to be empty. So, you need to add code that makes sure the method always returns something.

Adding return -1; in the end of the method, for example, would make the error go away in this case.



回答3:

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


标签: c# list main