🎓 All Courses | 📚 Blazor WASM Syllabus
Stickipedia University
📋 Study this course on TaskLoco

Generic components use the @typeparam directive to create components that work with any data type — similar to C# generics. Ideal for reusable data grids, lists, and dropdowns.

Defining a Generic Component

// GenericList.razor
@typeparam TItem

<ul>
    @foreach (var item in Items)
    {
        <li>@ItemTemplate(item)</li>
    }
</ul>

@code {
    [Parameter] public IEnumerable<TItem> Items { get; set; }
        = Enumerable.Empty<TItem>();
    [Parameter] public RenderFragment<TItem> ItemTemplate { get; set; } = default!;
}

Using It

<GenericList Items="products">
    <ItemTemplate Context="p">
        <strong>@p.Name</strong> — [email protected]
    </ItemTemplate>
</GenericList>

Type Constraints

@typeparam TItem where TItem : class, IIdentifiable

Type Inference

Blazor can often infer the type parameter from the data passed in — you don't usually need to specify it explicitly. If inference fails, pass it explicitly: <GenericList TItem="Product" ...>


YouTube • Top 10
Blazor WASM: Generic Components
Tap to Watch ›
📸
Google Images • Top 10
Blazor WASM: Generic Components
Tap to View ›

Reference:

Wikipedia: Generic Programming

image for linkhttps://en.wikipedia.org/wiki/Generic_programming

📚 Blazor WASM — Full Course Syllabus
📋 Study this course on TaskLoco

TaskLoco™ — The Sticky Note GOAT