Mantas
Mantas

Reputation: 191

Main: not all code paths return a value

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

Upvotes: 2

Views: 707

Answers (3)

01F0
01F0

Reputation: 1276

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.

Upvotes: 1

张静茹
张静茹

Reputation: 1

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

Upvotes: 0

Salah Akbari
Salah Akbari

Reputation: 39956

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

Upvotes: 3

Related Questions