HNHacker News
TopNewBestAskShowJobs

aar0nsa3nz

11 karma · joined June 27, 2019

submissionscomments
aar0nsa3nz··on C# Pattern Matching
Sorry, I couldn't figure out how to write a code block.
aar0nsa3nz··on C# Pattern Matching
With C# 8 you can also write it as:

  public static double ComputeArea(object shape) =>
    shape switch
    {
      Square s => s.Side * s.Side,
      Circle c => c.Radius * c.Radius * Math.PI,
      Rectangle r => r.Height * r.Length
      _ => throw new ArgumentException(
             message: "shape is not recognized",
             paramName: nameof(shape))
    };