Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/tests/detect_corpus/csharp/pattern_matching.txt

1.7 KiB, 1 run

created by r1870400018:11968, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1using System;
2
3namespace Annealer.Examples
4{
5 public abstract record Shape;
6 public record Circle(double Radius) : Shape;
7 public record Rectangle(double Width, double Height) : Shape;
8 public record Triangle(double Base, double Height) : Shape;
9
10 public static class Geometry
11 {
12 public static double Area(Shape shape) => shape switch
13 {
14 Circle c => Math.PI * c.Radius * c.Radius,
15 Rectangle r => r.Width * r.Height,
16 Triangle t => 0.5 * t.Base * t.Height,
17 _ => throw new ArgumentException(
18 $"unknown shape: {nameof(shape)}")
19 };
20
21 public static string Describe(Shape shape) => shape switch
22 {
23 Circle { Radius: > 10 } => "large circle",
24 Circle { Radius: > 0 } => "small circle",
25 Rectangle { Width: var w, Height: var h } when w == h
26 => $"square ({w}x{h})",
27 Rectangle r => $"rectangle ({r.Width}x{r.Height})",
28 Triangle t => $"triangle (base {t.Base})",
29 _ => "unknown"
30 };
31 }
32
33 class Program
34 {
35 static void Main()
36 {
37 Shape[] shapes = {
38 new Circle(5.0),
39 new Rectangle(4.0, 4.0),
40 new Triangle(3.0, 8.0),
41 new Circle(15.0),
42 };
43
44 foreach (var shape in shapes)
45 {
46 var desc = Geometry.Describe(shape);
47 var area = Geometry.Area(shape);
48 Console.WriteLine($"{desc}: area = {area:F2}");
49 }
50 }
51 }
52}