Skip to content

Commit f46a821

Browse files
committed
Add algorithm & shared method examples
1 parent 160e7fd commit f46a821

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

AlgorithmsLibrary.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<Compile Include="Components\AlgorithmsCore\BaseClass.cs" />
5050
<Compile Include="Components\AlgorithmsCore\BaseClassTools.cs" />
5151
<Compile Include="Components\Algorithms\Eratosthenes.cs" />
52+
<Compile Include="Components\Algorithms\Algorithm.example.cs" />
5253
<Compile Include="Components\Algorithms\Factorial.cs" />
5354
<Compile Include="Components\Algorithms\Fibonacci.cs" />
5455
<Compile Include="Components\Algorithms\Hanoi.cs" />
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
3+
// include this
4+
using AlgorithmsLibrary.AlgorithmsCore;
5+
6+
namespace AlgorithmsLibrary.Algorithms
7+
{
8+
9+
// derive from the "Algorithm" class
10+
internal class SuperAlgorithm : Algorithm
11+
{
12+
// private field, you can add as many as you want
13+
private string exclamationMark;
14+
15+
// override description to be displayed in the menu
16+
// (optional)
17+
public override string Description { get { return "I have the description!"; } }
18+
19+
// private method, you can add as many as you want
20+
private string GetMessage(string text)
21+
{
22+
return "Im the algorithm! " +
23+
text +
24+
exclamationMark +
25+
" 1 + 1 = " +
26+
// method defined in the AlgorithmsCore/BaseClassTools.cs
27+
OnePlusOne();
28+
}
29+
30+
// ovverride Display method to handle algorithm output
31+
// (optional, but required for functionality)
32+
public override void Display()
33+
{
34+
exclamationMark = "!";
35+
36+
Console.Write(GetMessage("Hooray"));
37+
}
38+
}
39+
}

Components/AlgorithmsCore/BaseClassTools.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,12 @@ protected bool IntParseTestWithOutput(string num)
3131
return false;
3232
}
3333
}
34+
35+
// example of the method to use across algorithms
36+
// you can use any return type or method name
37+
protected int OnePlusOne()
38+
{
39+
return 1 + 1;
40+
}
3441
}
3542
}

0 commit comments

Comments
 (0)