C# 9.0の範囲パターンのswitchはわりと賢くコンパイルされる
CRANK

範囲パターンを使ったswitch式C# 9.0 で switch式に比較演算を指定できるようになりました。元のコードpublic class C { public int Expr(int num) => num switch { <=0 => -1, < 10 => 0, < 20 => 1, < 30 => 2, < 40 => 3, < 50 => 4, < 60 => 5, _ => -1, }; } 上記のコードをコンパイル結果(のILをC#にデコンパイルしたもの)はこちら。コンパイル結果public class C { public int Expr(int num) { if (num < 30) { if (num < 10) { if (num <= 0) { return -1; } return 0; } if (num < 20) { return 1; } return 2; } if (num < 50) { if (num < 40) { return 3; } return 4; } if (num < 60) { r…

zenn.dev
Related Topics: C#
1 comments