Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor ComponentBase or Partial Class

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.

like image 950
Sean Avatar asked Aug 07 '26 12:08

Sean


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 
{
    ... 
}
like image 74
Henk Holterman Avatar answered Aug 10 '26 02:08

Henk Holterman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!