mirror of
https://github.com/Smaug123/WoofWare.PawPrint
synced 2025-10-20 20:58:40 +00:00
Implement Castclass
This commit is contained in:
40
WoofWare.PawPrint.Test/sourcesPure/IsinstPatternMatching.cs
Normal file
40
WoofWare.PawPrint.Test/sourcesPure/IsinstPatternMatching.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
public class Program
|
||||
{
|
||||
public abstract class Shape
|
||||
{
|
||||
public abstract double GetArea();
|
||||
}
|
||||
|
||||
public class Circle : Shape
|
||||
{
|
||||
public double Radius { get; set; }
|
||||
|
||||
public override double GetArea()
|
||||
{
|
||||
return 3.14 * Radius * Radius;
|
||||
}
|
||||
}
|
||||
|
||||
public class Square : Shape
|
||||
{
|
||||
public double Side { get; set; }
|
||||
|
||||
public override double GetArea()
|
||||
{
|
||||
return Side * Side;
|
||||
}
|
||||
}
|
||||
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
Shape shape = new Circle { Radius = 10 };
|
||||
|
||||
// Pattern matching uses isinst
|
||||
if (shape is Circle circle)
|
||||
{
|
||||
return (int)circle.Radius;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user