generics
Types that take other types as parameters, so one definition works for many kinds of value.
In more detail
A generic function or type has placeholders, such as T, that are filled in with real types where it is used. Generics let a library keep your specific types all the way through its code.
Examples
public static T[] Repeat<T>(T value, int count)ProcedureDef<TIn, TOut> fills TIn and TOut with each procedure’s real input and output types.Java: public class GenericBox<T> { private T value; }Java generics are checked at compile time and mostly erased at runtime.public class GenericBox<T> { private T value; }List<String> names = new ArrayList<>();function firstItem<T>(items: T[]): T | undefined { return items[0]; }new Map<string, number>() passes explicit type arguments because an empty collection gives nothing to infer fromRelated terms
- generic constraint — A rule on a type parameter that limits which types may fill it, such as requiring a class, a particular interface or a certain shape.
- List<T> — The .NET growable array: items keep their order, reading by index is constant time, and searching by value walks the whole list.
- boxing — Wrapping a value type such as an int in an object on the heap so it can be treated as an object.
- cast — An explicit conversion written (Type)value that tells the compiler to treat a value as another type, and throws at run time if it is not.
- TypeScript — JavaScript with added type annotations, checked by a compiler before the code runs.
- type inference — The compiler working out a type from the code itself, so you do not have to write it out by hand.
- bounded type parameter — A type parameter restricted to types that support a capability, such as T extends Comparable<T>, so the code can call that capability.
- generic wildcard — The question mark in a generic type, as in a list of some unknown subtype of Number, meaning some specific type that this code does not know.
- raw type — A generic type used without any type arguments, such as plain List, which turns off the compiler's type checks for it.
- type cast — An instruction to treat a value as a more specific type, written (String) value, which fails at runtime if the value is not that type.
- default type parameter — A type parameter with a fallback type, written <Data, Err = Error>, used when the caller does not supply one.
Taught in
- Collections, Generics, and LINQ — Modern .NET Core, C# & Enterprise Microservices
- The Options Pattern: Strongly Typed Configuration — Modern .NET Core, C# & Enterprise Microservices
- Type-Safe RPC Architectures: tRPC & Procedures — Production Full-Stack Engineering
- Environment Configuration: Secrets & Multi-Stage — Production Full-Stack Engineering
- Generics Fundamentals — Modern Java: Virtual Threads & High-Throughput Services
- Generics And Collections — Modern Java: Virtual Threads & High-Throughput Services
- Arrays: Declare, Sort, Search, Compare — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Generics, Bounds And Wildcards — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Exceptions, Arrays And Collections — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Lambdas, Functional Interfaces And Method References — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Why TypeScript: Contracts, Erased Types And Soundness — Production TypeScript
- Verify Your Environment: Running tsc And Bun TypeScript — Production TypeScript
- Literal Types: String, Number, Boolean Literals And const — Production TypeScript
- Custom Type Predicates: Functions Returning is Type — Production TypeScript
- Exhaustive Narrowing With never In Production Handlers — Production TypeScript
- Why Generics: Functions, Identities And Type Variables — Production TypeScript
- Generic Interfaces, Classes And Type Aliases — Production TypeScript
- Generic Constraints: extends, keyof And Indexed Access — Production TypeScript
- Default Generic Arguments And Multi-Type Parameters — Production TypeScript
- Const Type Parameters: Retaining Literal Types In Functions — Production TypeScript
- Conditional Types: Ternary Types And Type Inference With infer — Production TypeScript
- The satisfies Operator: Safe Validation Without Widening — Production TypeScript
- The Spine Artifact: Generating Typed Models From OpenAPI — Production TypeScript
References
- Generic types (C# fundamentals)
- Generics in .NET
- TypeScript: Generics
- Java Tutorials: Generics
- dev.java: Generics
- Java Language Specification, chapter 4: Types, Values, and Variables