-
-
Save dimitris-papadimitriou-chr/abbe9fa04008a6d2667e1467435c0a27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection.Metadata.Ecma335; | |
namespace PracticalCSharp.Preliminaries.PatternMatch.Test | |
{ | |
namespace Case1 | |
{ | |
abstract class Shape { } | |
class Rectangle : Shape | |
{ | |
public int Width { get; set; } | |
public int Height { get; set; } | |
} | |
class Circle : Shape | |
{ | |
public int Radius { get; set; } | |
} | |
public class Demo | |
{ | |
public void Run() | |
{ | |
// Func<Shape, string> GetShapeDescription = (Shape shape) => | |
string GetShapeDescription1(Shape shape) | |
{ | |
if (shape is Circle) | |
return $"found a circle with radius {(shape as Circle).Radius}"; | |
if (shape is Rectangle) | |
return $"Found {(shape as Rectangle).Height }x {(shape as Rectangle).Width} rectangle"; | |
throw new System.Exception(nameof(shape)); | |
} | |
string GetShapeDescription(Shape shape) | |
{ | |
switch (shape) | |
{ | |
case Circle c: | |
return $"found a circle with radius {c.Radius}"; | |
case Rectangle s: | |
return $"Found {s.Height }x {s.Width} rectangle"; | |
default: | |
throw new System.Exception(nameof(shape)); | |
} | |
}; | |
Shape shape = new Rectangle | |
{ | |
Width = 100, | |
Height = 100, | |
}; | |
var description = GetShapeDescription1(shape); | |
} | |
} | |
} | |
namespace Case2 | |
{ | |
abstract class Shape | |
{ | |
public abstract string GetShapeDescription(); | |
} | |
class Rectangle : Shape | |
{ | |
public int Width { get; set; } | |
public int Height { get; set; } | |
public override string GetShapeDescription() | |
{ | |
return $"Found { Height }x { Width} rectangle"; | |
} | |
} | |
class Circle : Shape | |
{ | |
public int Radius { get; set; } | |
public override string GetShapeDescription() | |
{ | |
return $"found a circle with radius { Radius}"; | |
} | |
} | |
public class Demo | |
{ | |
public void Run() | |
{ | |
Shape shape = new Rectangle { Width = 100, Height = 100, }; | |
shape = new Circle { Radius = 30 }; | |
var result = shape switch | |
{ | |
Rectangle { Width: var w, Height: var h } => $"Found {h}x {w} rectangle", | |
Circle { Radius: var r } => $"found a circle with radius {r}", | |
_ => "Different, or null shape" | |
}; | |
} | |
} | |
} | |
namespace Case3 | |
{ | |
abstract class Shape | |
{ | |
public abstract T MatchWith<T>(Func<Rectangle, T> rectangleCase, Func<Circle, T> circleCase); | |
} | |
class Rectangle : Shape | |
{ | |
public int Width { get; set; } | |
public int Height { get; set; } | |
public override T MatchWith<T>(Func<Rectangle, T> rectangleCase, Func<Circle, T> circleCase) | |
=> rectangleCase(this); | |
} | |
class Circle : Shape | |
{ | |
public int Radius { get; set; } | |
public override T MatchWith<T>(Func<Rectangle, T> rectangleCase, Func<Circle, T> circleCase) | |
=> circleCase(this); | |
} | |
public class Demo | |
{ | |
public void Run() | |
{ | |
Shape shape = new Rectangle { Width = 100, Height = 100, }; | |
shape = new Circle { Radius = 30 }; | |
string GetShapeDescription(Shape shape) | |
=> shape.MatchWith<string>( | |
rectangleCase: r => $"Found {r.Height}x {r.Width} rectangle", | |
circleCase: c => $"found a circle with radius {c.Radius}" | |
); | |
var description = GetShapeDescription(shape); | |
} | |
} | |
} | |
namespace Case4 | |
{ | |
abstract class Shape { } | |
class Rectangle : Shape | |
{ | |
public int Width { get; set; } | |
public int Height { get; set; } | |
} | |
class Circle : Shape | |
{ | |
public int Radius { get; set; } | |
} | |
public class Demo | |
{ | |
public void Run() | |
{ | |
// Func<Shape, string> GetShapeDescription = (Shape shape) => | |
// case Circle c: | |
// return $"found a circle with radius {c.Radius}"; | |
// case Rectangle s: | |
// return $"Found {s.Height }x {s.Width} rectangle"; | |
//default: | |
// throw new System.Exception(nameof(shape)); | |
string GetShapeDescription1(Shape shape) => | |
shape switch | |
{ | |
Rectangle { Height: var h, Width: var w } => $"Found {h }x {w} rectangle", | |
Circle { Radius: var r } => $"found a circle with radius {r}", | |
}; | |
Shape shape = new Rectangle | |
{ | |
Width = 100, | |
Height = 100, | |
}; | |
var description = GetShapeDescription1(shape); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment