/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
VS2017
samples/csharp/PatternMatching/GeometricUtilities.cs
162 строки
5 KB
Bill Wagner
Update pattern matching to fix a couple small issues (#1612)
06 мар 2017, 23:45
06 мар 2017, 23:45
d970e86
Код
Авторство
О чём код?
using System; using static System.Math; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PatternMatching { public static class GeometricUtilities { #region 04_ClassicSwitch public static string GenerateMessage(params string[] parts) { switch (parts.Length) { case 0: return "No elements to the input"; case 1: return $"One element: {parts[0]}"; case 2: return $"Two elements: {parts[0]}, {parts[1]}"; default: return $"Many elements. Too many to write"; } } #endregion #region 02_ClassicIsExpression public static double ComputeArea(object shape) { if (shape is Square) { var s = shape as Square; return s.Side * s.Side; } else if (shape is Circle) { var c = shape as Circle; return c.Radius * c.Radius * Math.PI; } // elided throw new ArgumentException( message: "shape is not a recognized shape", paramName: nameof(shape)); } #endregion #region 03_IsPatternExpression public static double ComputeAreaModernIs(object shape) { if (shape is Square s) return s.Side * s.Side; else if (shape is Circle c) return c.Radius * c.Radius * Math.PI; else if (shape is Rectangle r) return r.Height * r.Length; // elided throw new ArgumentException( message: "shape is not a recognized shape", paramName: nameof(shape)); } #endregion #region 05_SwitchTypePattern public static double ComputeAreaModernSwitch(object shape) { switch (shape) { case Square s: return s.Side * s.Side; case Circle c: return c.Radius * c.Radius * Math.PI; case Rectangle r: return r.Height * r.Length; default: throw new ArgumentException( message: "shape is not a recognized shape", paramName: nameof(shape)); } } #endregion #region 07_ComputeDegenerateShapes public static double ComputeArea_Version3(object shape) { switch (shape) { case Square s when s.Side == 0: case Circle c when c.Radius == 0: return 0; case Square s: return s.Side * s.Side; case Circle c: return c.Radius * c.Radius * Math.PI; default: throw new ArgumentException( message: "shape is not a recognized shape", paramName: nameof(shape)); } } #endregion #region 09_AddRectangleAndTriangle public static double ComputeArea_Version4(object shape) { switch (shape) { case Square s when s.Side == 0: case Circle c when c.Radius == 0: case Triangle t when t.Base == 0 || t.Height == 0: case Rectangle r when r.Length == 0 || r.Height == 0: return 0; case Square s: return s.Side * s.Side; case Circle c: return c.Radius * c.Radius * Math.PI; case Triangle t: return t.Base * t.Height * 2; case Rectangle r: return r.Length * r.Height; default: throw new ArgumentException( message: "shape is not a recognized shape", paramName: nameof(shape)); } } #endregion #region 10_NullCase public static double ComputeArea_Version5(object shape) { switch (shape) { case Square s when s.Side == 0: case Circle c when c.Radius == 0: case Triangle t when t.Base == 0 || t.Height == 0: case Rectangle r when r.Length == 0 || r.Height == 0: return 0; case Square s: return s.Side * s.Side; case Circle c: return c.Radius * c.Radius * Math.PI; case Triangle t: return t.Base * t.Height * 2; case Rectangle r: return r.Length * r.Height; case null: throw new ArgumentNullException(paramName: nameof(shape), message: "Shape must not be null"); default: throw new ArgumentException( message: "shape is not a recognized shape", paramName: nameof(shape)); } } #endregion } }