Skip to content

Commit 8b47b7a

Browse files
Updated mentoring.md for Wizard-and-Warriors exercise (#2364)
1 parent 02ba212 commit 8b47b7a

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Mentoring
2+
## Reasonable solutions
3+
4+
```csharp
5+
using System;
6+
7+
abstract class Character
8+
{
9+
protected string CharacterType { get; }
10+
11+
protected Character(string characterType)
12+
{
13+
CharacterType = characterType;
14+
}
15+
16+
public sealed override string ToString() => $"Character is a {CharacterType}";
17+
18+
public abstract int DamagePoints(Character target);
19+
20+
public virtual bool Vulnerable() => false;
21+
}
22+
23+
class Warrior : Character
24+
{
25+
public Warrior() : base("Warrior") { }
26+
27+
public override int DamagePoints(Character target) => target.Vulnerable() ? 10 : 6;
28+
}
29+
30+
class Wizard : Character
31+
{
32+
private bool SpellPrepared { get; set; } = false;
33+
34+
public Wizard() : base("Wizard") { }
35+
36+
public void PrepareSpell() => SpellPrepared = true;
37+
38+
public override bool Vulnerable() => !SpellPrepared;
39+
40+
public override int DamagePoints(Character target) => SpellPrepared ? 12 : 3;
41+
}
42+
```
43+
44+
45+
## Talking Points
46+
47+
### Encapsulation using properties
48+
- Use **properties** instead of fields to enhance encapsulation and readability.
49+
- Example: `CharacterType` and `SpellPrepared` are implemented as properties.
50+
51+
### Mark methods with sealed
52+
- Mark methods like `ToString` as `sealed` to prevent unnecessary overriding in derived classes.
53+
54+
### Expression-bodied members
55+
- Simplify methods with single-line logic using **expression-bodied members**.
56+
- Example:
57+
```csharp
58+
public override bool Vulnerable() => !SpellPrepared;
59+
```
60+
61+
### Use of readonly fields
62+
- Use `readonly` for fields that are initialized once and remain constant.
63+
- Example: `CharacterType` can be declared as `readonly`.
64+
65+
### Constructor Chaining
66+
- Use **constructor chaining** for clarity when initializing class members.
67+
- Example:
68+
```csharp
69+
public Warrior() : this("Warrior") { }
70+
```
71+
72+
### Abstract Classes and Polymorphism
73+
- The `Character` class is abstract, demonstrating the concept of polymorphism in C#.
74+
- Derived classes (`Warrior` and `Wizard`) implement their own behavior for `DamagePoints` and `Vulnerable`.
75+
76+
77+
78+
## Additional Resources
79+
80+
- [Properties (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties)
81+
- [sealed Modifier (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/sealed)
82+
- [Expression-bodied Members (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members)
83+
- [readonly (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly)
84+
- [Using Constructors (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors)
85+
- [Abstract Classes and Members (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract)

0 commit comments

Comments
 (0)