← All notes

C# generics and where constraints

Short answer

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

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).

License: CC-BY-4.0 · 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).

Cite this

Permanent ID: iil:note:gyykfcs4

iinventedlight. 2026. "C# generics and where constraints." iinventedlight Library, iil:note:gyykfcs4, version of 2026-11-04. https://iinventedlight.com/library/notes/csharp-generics/. License: CC-BY-4.0.

@misc{iil_note_gyykfcs4,
  author = {{iinventedlight}},
  title = {{C\# generics and where constraints}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-04},
  url = {https://iinventedlight.com/library/notes/csharp-generics/},
  note = {Permanent ID iil:note:gyykfcs4. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/csharp-generics.md.