What is the difference between writing your C# logic in an @code block versus using a code-behind partial class?
Last updated
Both compile to the same class. The difference is only organization:
@codekeeps logic inline, while a code-behindpartial classlives in a separate.razor.csfile.Inline suits small components. Code-behind suits complex ones (better tooling, testing, separation, injection via
[Inject]instead of@inject).
- https://learn.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-10.0
- https://claudiobernasconi.ch/blog/how-to-use-code-behind-for-blazor-components/
- https://medium.com/dotnet-new/advanced-blazor-component-techniques-for-clean-scalable-ui-development-7e8f03dd3e89
- https://github.com/dotnet/aspnetcore/discussions/57854
- https://www.telerik.com/blogs/using-a-code-behind-approach-to-blazor-components