1

I'm using a class like an enum because my entries need a custom string representation:

using System.Collections.Generic;

namespace MyProject
{
    internal class Food
    {
        private string _value = "";

        private Food(string value)
        {
            _value = value;
        }

        public override string ToString()
        {
            return _value;
        }

        internal static Food RedApple = new Food("red_apple");
        internal static Food YellowBanana = new Food("yellow_banana");
        internal static Food GreenMango = new Food("green_mango");
    }
}

I can use the static fields like Food.RedApple just fine:

if (str == Food.RedApple.ToString())
    Console.WriteLine("apple");
else if (str == Food.YellowBanana.ToString())
    Console.WriteLine("banana");
else if (str == Food.GreenMango.ToString())
    Console.WriteLine("mango");
else
    Console.WriteLine("unknown");

However, when I use them inside switch statements like so:

using System;

namespace MyProject
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "red_apple";

            switch (str)
            {
                case Food.RedApple.ToString():
                    Console.WriteLine("apple");
                    break;
                case Food.YellowBanana.ToString():
                    Console.WriteLine("banana");
                    break;
                case Food.GreenMango.ToString():
                    Console.WriteLine("mango");
                    break;
                default:
                    Console.WriteLine("unknown");
                    break;
            }
        }
    }
}

I get the following error:

The type name RedApple does not exist in the type Food [MyProject]

What exactly is going on here, and does this mean that I can't use my class inside switch statements?

Floating Sunfish
  • 4,920
  • 5
  • 29
  • 48

2 Answers2

1

Started from c# 7.0 switch statement has become more powerful and covers a lot of scenarios.

But to answer your question in shortest way you need to put a constant value in your case field. You can try putting a variable or another string property from Food inside switch statement, but it won't work either because switch is looking for a constant value.

You can look at the microsoft docs on switch statement.

SZT
  • 1,771
  • 4
  • 26
  • 53
  • I see... So C# `switch` statements need constant values. Welp, that clarifies everything. Thanks a lot for answering! – Floating Sunfish Feb 17 '20 at 09:40
  • Exactly, from c# 7.0 it has got more powerful, for example you can check against a type, but in general it requires a constant value – SZT Feb 17 '20 at 11:12
1

i'm not sure what exactly your case is, but in my case(using your code), i'm getting 'CS0150 A constant value is expected.' error message here.

the problem has been answered here: Switch case in C# - a constant value is expected

you might have a look at it first, it may be of some help.

Tu-Jen Liang
  • 123
  • 2
  • 7
  • Many thanks for the info! I like both answers, but could only choose one so I chose @SZT's because it's much closer to the issue. Thanks again for helping! – Floating Sunfish Feb 17 '20 at 09:39