diff --git a/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Abstract/Circle.cs b/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Abstract/Circle.cs new file mode 100644 index 0000000000..b7c04973c4 --- /dev/null +++ b/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Abstract/Circle.cs @@ -0,0 +1,12 @@ +namespace MethodOverridingInCSharp.Abstract +{ + public class Circle : Shape + { + public double Radius { get; set; } + + public override string Draw() + { + return $"Drawing a {Color} colored circle with a radius of {Radius} units."; + } + } +} diff --git a/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Abstract/Shape.cs b/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Abstract/Shape.cs new file mode 100644 index 0000000000..022787e8fe --- /dev/null +++ b/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Abstract/Shape.cs @@ -0,0 +1,8 @@ +namespace MethodOverridingInCSharp.Abstract +{ + public abstract class Shape + { + public required string Color { get; set; } + public abstract string Draw(); + } +} diff --git a/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Abstract/Square.cs b/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Abstract/Square.cs new file mode 100644 index 0000000000..68b1dffa7f --- /dev/null +++ b/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Abstract/Square.cs @@ -0,0 +1,13 @@ +namespace MethodOverridingInCSharp.Abstract +{ + public class Square : Shape + { + public int Side { get; set; } + + public override string Draw() + { + return $"Drawing a square of color {Color} " + + $"with its sides having length and width of {Side} units"; + } + } +} diff --git a/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/MethodOverridingInCSharp.csproj b/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/MethodOverridingInCSharp.csproj index f02677bf64..dfb40caafc 100644 --- a/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/MethodOverridingInCSharp.csproj +++ b/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/MethodOverridingInCSharp.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net10.0 enable enable diff --git a/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Program.cs b/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Program.cs index 6869fde077..1d1e018521 100644 --- a/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Program.cs +++ b/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Program.cs @@ -1,20 +1,19 @@ -namespace MethodOverridingInCSharp -{ - internal class Program - { - static void Main(string[] args) - { - var shape = new Shape { Color = "Blue" }; - Console.WriteLine(shape.Draw()); +using MethodOverridingInCSharp; - var circle = new Circle { Color = "Red", Radius = 10.5 }; - Console.WriteLine(circle.Draw()); +var shape = new Shape { Color = "Blue" }; +Console.WriteLine(shape.Draw()); - var square = new Square { Color = "Green", Side = 5 }; - Console.WriteLine(square.Draw()); +var circle = new Circle { Color = "Red", Radius = 10.5 }; +Console.WriteLine(circle.Draw()); - var cube = new Cube { Color = "Yellow", Edge = 7 }; - Console.WriteLine(cube.Draw()); - } - } -} \ No newline at end of file +var square = new Square { Color = "Green", Side = 5 }; +Console.WriteLine(square.Draw()); + +var cube = new Cube { Color = "Yellow", Edge = 7 }; +Console.WriteLine(cube.Draw()); + +var sketch = new Sketch { Color = "Black" }; +Console.WriteLine(sketch.Draw()); + +Shape sketchAsShape = sketch; +Console.WriteLine(sketchAsShape.Draw()); diff --git a/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Shape.cs b/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Shape.cs index 419572354e..398a5aad78 100644 --- a/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Shape.cs +++ b/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Shape.cs @@ -2,7 +2,7 @@ { public class Shape { - public string Color { get; set; } + public required string Color { get; set; } public virtual string Draw() { diff --git a/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Sketch.cs b/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Sketch.cs new file mode 100644 index 0000000000..98e86eac67 --- /dev/null +++ b/csharp-methods/MethodOverridingInCSharp/MethodOverridingInCSharp/Sketch.cs @@ -0,0 +1,10 @@ +namespace MethodOverridingInCSharp +{ + public class Sketch : Shape + { + public new string Draw() + { + return $"Sketching a {Color} outline"; + } + } +} diff --git a/csharp-methods/MethodOverridingInCSharp/Tests/AbstractTest.cs b/csharp-methods/MethodOverridingInCSharp/Tests/AbstractTest.cs new file mode 100644 index 0000000000..8314796e98 --- /dev/null +++ b/csharp-methods/MethodOverridingInCSharp/Tests/AbstractTest.cs @@ -0,0 +1,17 @@ +using MethodOverridingInCSharp.Abstract; + +namespace Tests +{ + public class AbstractTest + { + [Fact] + public void GivenAbstractBaseClass_WhenDerivedMethodsCalled_ThenCallDerivedImplementations() + { + var circle = new Circle { Color = "Red", Radius = 5.5 }; + var square = new Square { Color = "Blue", Side = 12 }; + + Assert.Equal("Drawing a Red colored circle with a radius of 5.5 units.", circle.Draw()); + Assert.Equal("Drawing a square of color Blue with its sides having length and width of 12 units", square.Draw()); + } + } +} diff --git a/csharp-methods/MethodOverridingInCSharp/Tests/BaseCallTest.cs b/csharp-methods/MethodOverridingInCSharp/Tests/BaseCallTest.cs new file mode 100644 index 0000000000..1745a3268f --- /dev/null +++ b/csharp-methods/MethodOverridingInCSharp/Tests/BaseCallTest.cs @@ -0,0 +1,21 @@ +using MethodOverridingInCSharp; + +namespace Tests +{ + public class BaseCallTest + { + [Fact] + public void GivenCubeInstance_WhenMethodCalled_ThenCallBaseMethodFirst() + { + var cube = new Cube { Color = "Yellow", Edge = 7 }; + + var lines = cube.Draw() + .ReplaceLineEndings("\n") + .Split('\n', StringSplitOptions.RemoveEmptyEntries); + + Assert.Equal(2, lines.Length); + Assert.Equal("Drawing a Yellow colored shape", lines[0]); + Assert.Equal("Drawing a cube with edges of length, width, and height of 7 units", lines[1]); + } + } +} diff --git a/csharp-methods/MethodOverridingInCSharp/Tests/HidingTest.cs b/csharp-methods/MethodOverridingInCSharp/Tests/HidingTest.cs new file mode 100644 index 0000000000..cb17e1e5a3 --- /dev/null +++ b/csharp-methods/MethodOverridingInCSharp/Tests/HidingTest.cs @@ -0,0 +1,37 @@ +using MethodOverridingInCSharp; + +namespace Tests +{ + public class HidingTest + { + [Fact] + public void GivenDerivedReference_WhenHiddenMethodCalled_ThenCallDerivedMethod() + { + var sketch = new Sketch { Color = "Black" }; + + var actual = sketch.Draw(); + + Assert.Equal("Sketching a Black outline", actual); + } + + [Fact] + public void GivenBaseReferenceToHidingObject_WhenMethodCalled_ThenCallBaseMethod() + { + Shape sketchAsShape = new Sketch { Color = "Black" }; + + var actual = sketchAsShape.Draw(); + + Assert.Equal("Drawing a Black colored shape", actual); + } + + [Fact] + public void GivenBaseReferenceToOverridingObject_WhenMethodCalled_ThenCallDerivedMethod() + { + Shape circleAsShape = new Circle { Color = "Black", Radius = 2 }; + + var actual = circleAsShape.Draw(); + + Assert.Equal("Drawing a Black colored circle with a radius of 2 units.", actual); + } + } +} diff --git a/csharp-methods/MethodOverridingInCSharp/Tests/Test.cs b/csharp-methods/MethodOverridingInCSharp/Tests/Test.cs index 0087f8cdf8..0d00daa143 100644 --- a/csharp-methods/MethodOverridingInCSharp/Tests/Test.cs +++ b/csharp-methods/MethodOverridingInCSharp/Tests/Test.cs @@ -10,7 +10,7 @@ public void GivenBaseClassInstance_WhenMethodCalled_ThenCallBaseClassMethod() var shape = new Shape { Color = "Black" }; var actual = shape.Draw(); - var expected = string.Format("Drawing a Black colored shape"); + var expected = "Drawing a Black colored shape"; Assert.Equal(expected, actual); } @@ -21,7 +21,7 @@ public void GivenDerivedClassInstance_WhenMethodCalled_ThenCallDerivedClassMetho var circle = new Circle { Color = "Gray", Radius = 22.7 }; var actual = circle.Draw(); - var expected = string.Format("Drawing a Gray colored circle with a radius of 22.7 units."); + var expected = "Drawing a Gray colored circle with a radius of 22.7 units."; Assert.Equal(expected, actual); } diff --git a/csharp-methods/MethodOverridingInCSharp/Tests/Tests.csproj b/csharp-methods/MethodOverridingInCSharp/Tests/Tests.csproj index 1e6b8e465a..c01d90a313 100644 --- a/csharp-methods/MethodOverridingInCSharp/Tests/Tests.csproj +++ b/csharp-methods/MethodOverridingInCSharp/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable @@ -9,13 +9,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all