Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MethodOverridingInCSharp.Abstract
{
public abstract class Shape
{
public required string Color { get; set; }
public abstract string Draw();
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
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());
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class Shape
{
public string Color { get; set; }
public required string Color { get; set; }

public virtual string Draw()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MethodOverridingInCSharp
{
public class Sketch : Shape
{
public new string Draw()
{
return $"Sketching a {Color} outline";
}
}
}
17 changes: 17 additions & 0 deletions csharp-methods/MethodOverridingInCSharp/Tests/AbstractTest.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
21 changes: 21 additions & 0 deletions csharp-methods/MethodOverridingInCSharp/Tests/BaseCallTest.cs
Original file line number Diff line number Diff line change
@@ -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]);
}
}
}
37 changes: 37 additions & 0 deletions csharp-methods/MethodOverridingInCSharp/Tests/HidingTest.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
4 changes: 2 additions & 2 deletions csharp-methods/MethodOverridingInCSharp/Tests/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions csharp-methods/MethodOverridingInCSharp/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="10.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading