---
source: https://iinventedlight.com/library/notes/csharp-generics.md
title: "C# generics and where constraints"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "CC BY 4.0 (dotnet/docs); prose restates cited source lines"
attribution: "Summary by iinventedlight (AI-assisted). Sources: Microsoft Learn (.NET docs), Generic types and methods - C#, CC BY 4.0 (dotnet/docs); Microsoft Learn (.NET docs), where (generic type constraint) - C# reference, CC BY 4.0 (dotnet/docs)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: C# and .NET"
tags: csharp, dotnet, generics
sources:
  - id: S1
    title: "Generic types and methods - C#"
    publisher: Microsoft Learn (.NET docs)
    url: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/generics
    accessed: 2026-11-04
    license: "CC BY 4.0 (dotnet/docs)"
  - id: S2
    title: "where (generic type constraint) - C# reference"
    publisher: Microsoft Learn (.NET docs)
    url: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/where-generic-type-constraint
    accessed: 2026-11-04
    license: "CC BY 4.0 (dotnet/docs)"
---
# C# generics and where constraints

## Short answer

```csharp
T Max<T>(T a, T b) where T : IComparable<T> => a.CompareTo(b) >= 0 ? a : b;
var m = Max(3, 7); // T inferred as int
```

## Key points
- Generic collections avoid boxing value types, which helps performance. [S1]
- The compiler often infers type arguments from the values you pass. [S1]
- Constraints can require an interface, a base class, or kinds like class or struct. [S2]

## Claims
- id: C1
  claim: Generics keep full type safety while working with any type.
  source: S1
  evidence: "Generics let you write code that works with any type while keeping full type safety."
- id: C2
  claim: Generic collections avoid boxing for value types.
  source: S1
  evidence: "These collections also avoid boxing for value types"
- id: C3
  claim: The compiler often infers the type argument.
  source: S1
  evidence: "The compiler often infers the type argument from the values you pass"
- id: C4
  claim: The where clause specifies constraints on type arguments.
  source: S2
  evidence: "use the where clause to specify constraints on the types"

## Sources
- [S1] Generic types and methods - C#. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/generics . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.
- [S2] where (generic type constraint) - C# reference. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/where-generic-type-constraint . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
