2

I've always thought that a method different than void must return a value no matter what however if we have this for example

    private int Foo()
    {
        while (true)
        {

        }
    }

There's no compile-time errors here and I think that's because the loop is infinite C# can guarantee that it wont terminate without returning a value. Am I correct ?

2 Answers2

2

Yes, if the function is going to run forever the return value is redundant so the compiler just ignore it.

Dr.Haimovitz
  • 1,568
  • 12
  • 16
0

Only a non-void function with reachable end point has to return a value.

For more info, check this answer by Eric Lippert.

Community
  • 1
  • 1
Ahmad Ibrahim
  • 1,915
  • 2
  • 15
  • 32