Switch case in C# - a constant value is expected

前端 未结 7 956
太阳男子
太阳男子 2020-11-27 05:50

My code is as follows:

public static void Output(IEnumerable dataSource) where T : class
{   
    dataSourceName = (typeof(T).Name);
    sw         


        
相关标签:
7条回答
  • 2020-11-27 05:50

    This seems to work for me at least when i tried on visual studio 2017.

    public static class Words
    {
         public const string temp = "What";
         public const string temp2 = "the";
    }
    var i = "the";
    
    switch (i)
    {
      case Words.temp:
        break;
      case Words.temp2:
        break;
    }
    
    0 讨论(0)
  • 2020-11-27 05:54

    You can't use a switch statement for this as the case values cannot be evaluated expressions. For this you have to use an an if/else ...

    public static void Output<T>(IEnumerable<T> dataSource) where T : class
    {   
        dataSourceName = (typeof(T).Name);
        if(string.Compare(dataSourceName, typeof(CustomerDetails).Name.ToString(), true)==0)
        {
            var t = 123;
        }
        else if (/*case 2 conditional*/)
        {
            //blah
        }
        else
        {
            //default case
            Console.WriteLine("Test");
        }
    }
    

    I also took the liberty of tidying up your conditional statement. There is no need to cast to string after calling ToString(). This will always return a string anyway. When comparing strings for equality, bare in mind that using the == operator will result in a case sensitive comparison. Better to use string compare = 0 with the last argument to set case sensitive on/off.

    0 讨论(0)
  • 2020-11-27 05:56

    See C# switch statement limitations - why?

    Basically Switches cannot have evaluated statements in the case statement. They must be statically evaluated.

    0 讨论(0)
  • 2020-11-27 06:02

    You can only match to constants in switch statements.


    Example:

    switch (variable1)
    {
        case 1: // A hard-coded value
            // Code
            break;
        default:
            // Code
            break;
    }
    

    Successful!


    switch (variable1)
    {
        case variable2:
            // Code
            break;
        default:
            // Code
            break;
    }
    

    CS0150 A constant value is expected.

    0 讨论(0)
  • 2020-11-27 06:05

    Now you can use nameof:

    public static void Output<T>(IEnumerable<T> dataSource) where T : class
    {
        string dataSourceName = typeof(T).Name;
        switch (dataSourceName)
        {
            case nameof(CustomerDetails):
                var t = 123;
                break;
            default:
                Console.WriteLine("Test");
        }
    }
    

    nameof(CustomerDetails) is basically identical to the string literal "CustomerDetails", but with a compile-time check that it refers to some symbol (to prevent a typo).

    nameof appeared in C# 6.0, so after this question was asked.

    0 讨论(0)
  • 2020-11-27 06:09

    switch is very picky in the sense that the values in the switch must be a compile time constant. and also the value that's being compared must be a primitive (or string now). For this you should use an if statement.

    The reason may go back to the way that C handles them in that it creates a jump table (because the values are compile time constants) and it tries to copy the same semantics by not allowing evaluated values in your cases.

    0 讨论(0)
提交回复
热议问题