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

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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...