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.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...