Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

blazor - EditorRequired attribute

 @code {
     [Parameter, EditorRequired] 
     public Trail Trail { get; set; } = default!;
 }

What does this mean

  1. EditorRequired
  2. public Trail Trail { get; set; } = default!; (why default and why exclamatory mark?)
like image 988
Venkat Avatar asked Jun 11 '26 18:06

Venkat


1 Answers

What does this mean

  1. EditorRequired

It is a hint to the user of your component that Trail is a required parameter. When you 'forget' Trail, as in <MyComponent /> you will get a warning, and some squiggly lines under that usage.
The warning in your Build output is

RZ2012 Component 'MyComponent' expects a value for the parameter 'Trail', but a value may not have been provided.

  1. public Trail Trail { get; set; } = default!;
    (why default and why exclamatory mark?)

This is a way to reduce warnings. When you omit = default!; you will get the warning:

CS8618 Non-nullable property 'Trail' must contain a non-null value when exiting constructor.

Together these two features provide a reasonable way to work with nullable reference types in Blazor. It is not air-tight, you can still get null-reference exceptions. But not as easily anymore.

What you have here is a standard pattern and a best-practice.

like image 104
Henk Holterman Avatar answered Jun 13 '26 06:06

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!