-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkod2_Switch Expression + pattern matching.cs
More file actions
37 lines (32 loc) · 1.43 KB
/
kod2_Switch Expression + pattern matching.cs
File metadata and controls
37 lines (32 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.Diagnostics;
using System.Reflection.Metadata.Ecma335;
using System.Reflection.PortableExecutable;
namespace testing
{
record Circle(double Radius);
record Rectangle(double Width, double Height);
internal class Program
{
/*Switch expression + pattern matching
Напиши метод, который принимает object shape и возвращает площадь(double) :
Если Circle с Radius — π* r²
Если Rectangle с Width и Height — w* h
Иначе 0
Определи классы:record Circle(double Radius);
record Rectangle(double Width, double Height); Используй pattern matching в switch expression.*/
static double findSquare(object shape) => shape switch
{
Circle circle => Math.PI * circle.Radius * circle.Radius,
Rectangle rec => rec.Width * rec.Height,
_ => 0
};
static void Main(string[] args)
{
var circle = new Circle(5); // радиус 5
var rect = new Rectangle(4, 6); // ширина 4, высота 6
Console.WriteLine("Площадь круга: " + findSquare(circle)); // ≈78.54
Console.WriteLine("Площадь прямоугольника: " + findSquare(rect)); // 24
Console.WriteLine("Площадь неизвестной фигуры: " + findSquare("не фигура")); // 0
}
}
}