Is there a better alternative than this to 'switch on type'?

后端 未结 30 2540
梦毁少年i
梦毁少年i 2020-11-22 03:28

Seeing as C# can\'t switch on a Type (which I gather wasn\'t added as a special case because is relationships mean that more than one distinct

30条回答
  •  难免孤独
    2020-11-22 04:00

    Should work with

    case type _:

    like:

    int i = 1;
    bool b = true;
    double d = 1.1;
    object o = i; // whatever you want
    
    switch (o)
    {
        case int _:
            Answer.Content = "You got the int";
            break;
        case double _:
            Answer.Content = "You got the double";
            break;
        case bool _:
            Answer.Content = "You got the bool";
            break;
    }
    

提交回复
热议问题