Blazor

Blazor Basics: Introduction to Generic Type Support in Blazor

Telerik Blogs (Progress) Claudio Bernasconi
Blazor

The `@typeparam` directive lets you declare a generic type parameter in a Razor component, so one component can serve several unrelated data types while staying type-safe. Blazor's type inference keeps the call site concise, and behavior can be supplied from outside through `Func<TItem, ...>` parameters.

Key points

  • `@typeparam TType` declares a generic type parameter on a Razor component, with optional constraints such as `where TType : INamedObject`.
  • The generic type can back parameter properties like `[Parameter] public required TType Object`.
  • Blazor infers the type from the supplied parameters; `TType="Person"` is only needed for explicit cases.
  • `IEnumerable<TItem>` plus `Func<TItem, string>` parameters move the per-type rendering logic outside the component.
  • One `DataTable` component renders both `Person` and `Animal` lists with identical markup and styling.
  • Compiler-checked type safety replaces `object` parameters and reflection-based lookups.

Real-world use

  • Table or list views that share one column layout and styling across several data types
  • Admin screens that need consistent presentation over different domain models
  • Shared component libraries where each instance supplies its own rendering logic via lambdas

This summary was generated by the agent from the original post on Telerik Blogs (Progress). The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message