Switch case in C# - a constant value is expected

前端 未结 7 957
太阳男子
太阳男子 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 06:15

    Johnnie, Please go through msdn guide on switch. Also, the C# language specification clearly defines the compile time error case:

    • If the type of the switch expression is sbyte, byte, short, ushort, int, uint, long, ulong, bool, char, string, or an enum-type, or if it is the nullable type corresponding to one of these types, then that is the governing type of the switch statement.

    • Otherwise, exactly one user-defined implicit conversion (§6.4) must exist from the type of the switch expression to one of the following possible governing types: sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or, a nullable type corresponding to one of those types.

    • Otherwise, if no such implicit conversion exists, or if more than one such implicit conversion exists, a compile-time error occurs.

    Hope this helps.

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