Studies show that good grammar leads to more promotions and higher salaries. Write with Grammarly

C# Error CS0161 – ‘method’: not all code paths return a value

C# Compiler Error

CS0161 – ‘method’: not all code paths return a value

Reason for the Error

You will receive this error in C# when you don’t return a value for a method whose return type is a non-void type.

For example, take a look at the below example.

namespace ConsoleApp2
{
    class Program
    {
        public static int Main()
        {

        }

    }
    
}

We have a method Main() which has a return type of int. We will receive the C# error code CS0161 since no value is returned from the Main method.

C# Error CS0161 – 'method': not all code paths return a value

Error CS0161 ‘Program.Main()’: not all code paths return a value ConsoleApp2 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp2\Program.cs 5 Active

Solution

If a method has a return type, it needs to return a value. To fix the above error, ensure that Main method returns a value.