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

后端 未结 30 2557
梦毁少年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:11

    With C# 7, which shipped with Visual Studio 2017 (Release 15.*), you are able to use Types in case statements (pattern matching):

    switch(shape)
    {
        case Circle c:
            WriteLine($"circle with radius {c.Radius}");
            break;
        case Rectangle s when (s.Length == s.Height):
            WriteLine($"{s.Length} x {s.Height} square");
            break;
        case Rectangle r:
            WriteLine($"{r.Length} x {r.Height} rectangle");
            break;
        default:
            WriteLine("");
            break;
        case null:
            throw new ArgumentNullException(nameof(shape));
    }
    

    With C# 6, you can use a switch statement with the nameof() operator (thanks @Joey Adams):

    switch(o.GetType().Name) {
        case nameof(AType):
            break;
        case nameof(BType):
            break;
    }
    

    With C# 5 and earlier, you could use a switch statement, but you'll have to use a magic string containing the type name... which is not particularly refactor friendly (thanks @nukefusion)

    switch(o.GetType().Name) {
      case "AType":
        break;
    }
    

提交回复
热议问题