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