Skip to content

Commit 908ee8d

Browse files
committed
Complete 03-advanced parity with templates-basics for C#, Go, and Python
1 parent 5edcf6c commit 908ee8d

26 files changed

Lines changed: 492 additions & 17 deletions

File tree

LANGUAGE_PARITY_MATRIX.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This matrix tracks concept parity across C++, C#, Go, and Python.
44

55
- Canonical order is defined by the C++ track.
6-
- Current priority is to finish `03-advanced` parity by implementing `templates-basics`.
6+
- Current priority is to define the next parity step after completing `03-advanced`.
77
- Status labels:
88
- `Done`: module implemented with example, exercises, and README.
99
- `Planned`: module not implemented yet, already queued in order.
@@ -42,9 +42,9 @@ Current parity progress in non-C++ tracks:
4242

4343
Current parity progress in non-C++ tracks:
4444

45-
- C#: `4/5` modules complete in `03-advanced`
46-
- Go: `4/5` modules complete in `03-advanced`
47-
- Python: `4/5` modules complete in `03-advanced`
45+
- C#: `5/5` modules complete in `03-advanced`
46+
- Go: `5/5` modules complete in `03-advanced`
47+
- Python: `5/5` modules complete in `03-advanced`
4848

4949
### Advanced (`03-advanced`) - Current Expansion Queue
5050

@@ -54,6 +54,6 @@ Current parity progress in non-C++ tracks:
5454
| 2 | constructors-and-invariants | Done | Done | Done | Done |
5555
| 3 | copy-and-move-semantics | Done | Done | Done | Done |
5656
| 4 | inheritance-and-polymorphism | Done | Done | Done | Done |
57-
| 5 | templates-basics | Done | Planned | Planned | Planned |
57+
| 5 | templates-basics | Done | Done | Done | Done |
5858

5959
`04-expert` remains C++ only for now.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ This repository teaches programming through small runnable examples and focused
2525
| Language | Current Levels | Module Coverage | Track Status |
2626
| --- | --- | --- | --- |
2727
| C++ | 00-setup, 01-foundations, 02-core, 03-advanced, 04-expert | Foundations, Core, Advanced, Expert, projects, assessments | Most complete and primary track |
28-
| C# | 01-foundations, 02-core, 03-advanced | 8/8 foundations modules, 6/6 core modules, 4/5 advanced modules | Advanced parity in progress |
29-
| Go | 01-foundations, 02-core, 03-advanced | 8/8 foundations modules, 6/6 core modules, 4/5 advanced modules | Advanced parity in progress |
30-
| Python | 01-foundations, 02-core, 03-advanced | 8/8 foundations modules, 6/6 core modules, 4/5 advanced modules | Advanced parity in progress |
28+
| C# | 01-foundations, 02-core, 03-advanced | 8/8 foundations modules, 6/6 core modules, 5/5 advanced modules | Advanced parity complete |
29+
| Go | 01-foundations, 02-core, 03-advanced | 8/8 foundations modules, 6/6 core modules, 5/5 advanced modules | Advanced parity complete |
30+
| Python | 01-foundations, 02-core, 03-advanced | 8/8 foundations modules, 6/6 core modules, 5/5 advanced modules | Advanced parity complete |
3131

3232
Parity planning reference: [LANGUAGE_PARITY_MATRIX.md](LANGUAGE_PARITY_MATRIX.md)
3333

languages/csharp/03-advanced/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ This level starts advanced object modeling and type design.
88
2. [constructors-and-invariants](./constructors-and-invariants/README.md)
99
3. [copy-and-move-semantics](./copy-and-move-semantics/README.md)
1010
4. [inheritance-and-polymorphism](./inheritance-and-polymorphism/README.md)
11+
5. [templates-basics](./templates-basics/README.md)
1112

1213
Track progress in [../CHECKLIST.md](../CHECKLIST.md).
1314

1415
## Study Tip
1516

16-
Use `inheritance-and-polymorphism` after `copy-and-move-semantics` to model behavior variation through a clean base contract.
17+
Use `templates-basics` last to generalize what you already modeled with concrete types and inheritance.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Templates Basics (C#)
2+
3+
This module introduces generic programming with methods and classes.
4+
5+
## Quick Run
6+
7+
~~~bash
8+
dotnet run --project example/templates-basics-example.csproj
9+
~~~
10+
11+
## Topics Covered
12+
13+
- Generic methods with type constraints.
14+
- Generic classes for reusable containers.
15+
- Type-independent logic with `T`.
16+
- When constraints are required for comparisons and arithmetic-like behavior.
17+
18+
## Common Pitfalls
19+
20+
- Assuming all generic types support comparison automatically.
21+
- Using `object` instead of generics and losing type safety.
22+
- Hiding constraints until compile errors become confusing.
23+
24+
## Exercise Focus
25+
26+
- exercises/01.cs: generic swap function.
27+
- exercises/02.cs: generic average over numeric lists.
28+
29+
### Exercise Specs
30+
31+
1. exercises/01.cs
32+
- Input: two values of the same type.
33+
- Output: values before and after swap.
34+
- Edge cases: identical values; negative numbers.
35+
36+
2. exercises/02.cs
37+
- Input: numeric sequence.
38+
- Output: arithmetic average.
39+
- Edge cases: empty list should return `0`; mixed positive/negative values.
40+
41+
## Checkpoint
42+
43+
- [ ] I can write and call generic methods.
44+
- [ ] I can create simple generic classes.
45+
- [ ] I can identify where generic constraints are required.
46+
- [ ] I completed exercises/01.cs.
47+
- [ ] I completed exercises/02.cs.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Example purpose: show the module flow with clear, beginner-friendly steps.
2+
3+
using System;
4+
5+
static class Helpers
6+
{
7+
public static T MaxValue<T>(T left, T right) where T : IComparable<T>
8+
{
9+
return left.CompareTo(right) > 0 ? left : right;
10+
}
11+
}
12+
13+
class Pair<T>
14+
{
15+
private readonly T first;
16+
private readonly T second;
17+
18+
public Pair(T firstValue, T secondValue)
19+
{
20+
first = firstValue;
21+
second = secondValue;
22+
}
23+
24+
public void Print()
25+
{
26+
// Intent: print final state from a generic container in one predictable format.
27+
Console.WriteLine($"({first}, {second})");
28+
}
29+
}
30+
31+
class Program
32+
{
33+
static void Main()
34+
{
35+
// Program flow: call one generic method, then inspect one generic class instance.
36+
Console.WriteLine($"MaxValue(4, 7) = {Helpers.MaxValue(4, 7)}");
37+
Console.WriteLine($"MaxValue(2.5, 1.2) = {Helpers.MaxValue(2.5, 1.2)}");
38+
39+
Pair<string> pair = new Pair<string>("left", "right");
40+
pair.Print();
41+
}
42+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>disable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
</Project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
3+
static class Helpers
4+
{
5+
public static void SwapValues<T>(ref T left, ref T right)
6+
{
7+
T temp = left;
8+
left = right;
9+
right = temp;
10+
}
11+
}
12+
13+
class Program
14+
{
15+
static void Main()
16+
{
17+
Console.Write("Enter two integers: ");
18+
string raw = Console.ReadLine() ?? string.Empty;
19+
string[] parts = raw.Split(' ', StringSplitOptions.RemoveEmptyEntries);
20+
21+
if (parts.Length != 2 ||
22+
!int.TryParse(parts[0], out int left) ||
23+
!int.TryParse(parts[1], out int right))
24+
{
25+
Console.WriteLine("Invalid input.");
26+
return;
27+
}
28+
29+
Console.WriteLine($"Before swap: {left} {right}");
30+
Helpers.SwapValues(ref left, ref right);
31+
Console.WriteLine($"After swap: {left} {right}");
32+
}
33+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
static class Helpers
5+
{
6+
public static double AverageOf<T>(IReadOnlyList<T> values) where T : IConvertible
7+
{
8+
if (values.Count == 0)
9+
{
10+
return 0.0;
11+
}
12+
13+
double sum = 0.0;
14+
foreach (T value in values)
15+
{
16+
sum += value.ToDouble(System.Globalization.CultureInfo.InvariantCulture);
17+
}
18+
19+
return sum / values.Count;
20+
}
21+
}
22+
23+
class Program
24+
{
25+
static void Main()
26+
{
27+
Console.Write("How many numbers? ");
28+
if (!int.TryParse(Console.ReadLine(), out int count) || count < 0)
29+
{
30+
Console.WriteLine("Please enter zero or a positive count.");
31+
return;
32+
}
33+
34+
List<double> values = new List<double>(count);
35+
for (int index = 0; index < count; index++)
36+
{
37+
string rawValue = Console.ReadLine() ?? string.Empty;
38+
if (!double.TryParse(rawValue, out double parsed))
39+
{
40+
Console.WriteLine("Invalid number.");
41+
return;
42+
}
43+
44+
values.Add(parsed);
45+
}
46+
47+
Console.WriteLine($"Average: {Helpers.AverageOf(values)}");
48+
}
49+
}

languages/csharp/CHECKLIST.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- [ ] Complete `03-advanced/constructors-and-invariants`.
2727
- [ ] Complete `03-advanced/copy-and-move-semantics`.
2828
- [ ] Complete `03-advanced/inheritance-and-polymorphism`.
29+
- [ ] Complete `03-advanced/templates-basics`.
2930

3031
## Parity Goals
3132

languages/csharp/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
## Scope and Status
44

5-
This track currently covers `01-foundations`, `02-core`, and early `03-advanced`.
5+
This track currently covers `01-foundations`, `02-core`, and `03-advanced`.
66

77
- 8/8 foundations modules implemented.
88
- 6/6 core modules implemented (`input-validation`, `algorithms-basics`, `file-io-basics`, `sorting-and-searching`, `maps-and-frequency-counting`, `error-handling-and-defensive-programming`).
9-
- 4/5 advanced modules implemented (`structs-and-classes`, `constructors-and-invariants`, `copy-and-move-semantics`, `inheritance-and-polymorphism`).
9+
- 5/5 advanced modules implemented (`structs-and-classes`, `constructors-and-invariants`, `copy-and-move-semantics`, `inheritance-and-polymorphism`, `templates-basics`).
1010
- Same module naming as C++, Python, and Go for parity.
1111

1212
## Prerequisites
@@ -50,6 +50,7 @@ dotnet run --project 01-foundations/types-and-io/example/types-and-io-example.cs
5050
- [constructors-and-invariants](./03-advanced/constructors-and-invariants/README.md)
5151
- [copy-and-move-semantics](./03-advanced/copy-and-move-semantics/README.md)
5252
- [inheritance-and-polymorphism](./03-advanced/inheritance-and-polymorphism/README.md)
53+
- [templates-basics](./03-advanced/templates-basics/README.md)
5354

5455
## Progress Tracking
5556

0 commit comments

Comments
 (0)