Blazor ComponentBase or Partial Class

Viewed 3035

Are there any big differences or advantages to using ComponentBase over Partial Class or vice versa in Blazor? I've checked the documentations from Microsoft and so far I didn't get a clear answer. Searching over the internet didn't give me much neither. If there is already a similar question, pointing me to it would be great as well.

1 Answers

differences or advantages to using ComponentBase over Partial Class

Those are not two different or opposite things. You can do both at the same time.

When you add a "code behind class" to for instance the Counter page:

// Counter.razor.cs
partial class Counter
{
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }
}

you are using a default rule for partial classes. The above definition is fully equivalent to

// Counter.razor.cs
public partial class Counter : ComponentBase 
{
    ... 
}
Related