C# Error CS8119 – The switch expression must be a value; found ‘{0}’

C# Compiler Error

CS8119 – The switch expression must be a value; found ‘{0}’

Reason for the Error

You will receive this error in C# when you have used method that returns a void instead of a value.

For example, the below code snippet will result in the CS8119 error code in C#.

namespace DeveloperPublishNamespace
{
   public class DeveloperPublish 
    {
        public static void Main()
        {

            switch (GetData())
            {
                case 1:
                    break;
            }
        }
        public static void GetData()
        {

        }
    }

}

Error CS8119 The switch expression must be a value; found ‘void’. ConsoleApp1 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 8 Active

C# Error CS8119 – The switch expression must be a value; found '{0}'

The same code snippet would return a different error code depending on the version of the .NET (Mono for example will result with the below error).

Main.cs(8,13): error CS0151: A switch expression of type `void’ cannot be converted to an integral type, bool, char, string, enum or nullable type

Solution

To fix the error, change the data type of the function from void to return a value to use it with-in the switch statement in C#.

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...