Skip to content

Latest commit

 

History

History
60 lines (42 loc) · 1.9 KB

File metadata and controls

60 lines (42 loc) · 1.9 KB

Templates Basics (C#)

This module introduces generic programming with methods and classes.

Learning Metadata

  • Difficulty: Intermediate.
  • Estimated Time: 35-50 minutes.
  • Prerequisites: 01-foundations/functions, 03-advanced/structs-and-classes.
  • Cross-Language Lens: Compare C++ templates with C# generics, Go type parameters, and Python's runtime flexibility.

Quick Run

dotnet run --project example/templates-basics-example.csproj

Topics Covered

  • Generic methods with type constraints.
  • Generic classes for reusable containers.
  • Type-independent logic with T.
  • When constraints are required for comparisons and arithmetic-like behavior.

Common Pitfalls

  • Assuming all generic types support comparison automatically.
  • Using object instead of generics and losing type safety.
  • Hiding constraints until compile errors become confusing.

Cross-Language Notes

  • Compared with C++, this concept broadens from templates into each language's own reusable generic abstraction model.
  • Relative to Go and C#, TypeScript generics stay expressive without runtime specialization, while Python treats the same idea more informally.
  • The useful comparison is how each language generalizes logic without abandoning type clarity.

Exercise Focus

  • exercises/01.cs: generic swap function.
  • exercises/02.cs: generic average over numeric lists.

Exercise Specs

  1. exercises/01.cs
  • Input: two values of the same type.
  • Output: values before and after swap.
  • Edge cases: identical values; negative numbers.
  1. exercises/02.cs
  • Input: numeric sequence.
  • Output: arithmetic average.
  • Edge cases: empty list should return 0; mixed positive/negative values.

Checkpoint

  • I can write and call generic methods.
  • I can create simple generic classes.
  • I can identify where generic constraints are required.
  • I completed exercises/01.cs.
  • I completed exercises/02.cs.