2

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;
                        }
                    }
                }
            }
        }
    }
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Mantas
  • 199
  • 2
  • 12
  • If your doc.Parts is empty,you will miss your return 0.Add anything in the last line of the method code – Suren Srapyan Nov 13 '15 at 09:48
  • 1
    Off-topic but is there a reason you need nested classes? ... it looks like it is already getting messy – Sayse Nov 13 '15 at 09:49
  • yes, I will need them in future use – Mantas Nov 13 '15 at 09:50
  • 2
    @Mantas: You *need* them to be nested classes? That sounds unlikely... – Jon Skeet Nov 13 '15 at 09:51
  • 1
    If the method signature says it will return something, it has to do it in all conditions. If your list is empty, method will not return anything. So, you might return a default value at the end of the method, which will fix the compile error. Also, notice that, you are returning in a loop. So, in the first iteration itself the method will return, and the rest of the loop will not be executed. – Arghya C Nov 13 '15 at 09:57
  • Possible duplicate of [c# returning error "not all code paths return a value"](http://stackoverflow.com/questions/21197410/c-sharp-returning-error-not-all-code-paths-return-a-value) – Liam Jun 14 '16 at 10:01

3 Answers3

3

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;
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
1

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.

01F0
  • 1,228
  • 2
  • 19
  • 32
0
 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;
            }
张静茹
  • 1
  • 2