I'm creating a Blazor component and running into nullable type issues
MyComponentTest.razor(3,36): error CS0407: 'int? MyComponentTest.DivideByFour(string?)' has the wrong return type
In MyComponentTest.razor, it's expecting me to pass a Func<string, T> to ToValueFunc, even though in MyComponent.razorToValueFunc is defined as being Func<string?, T?>.
Three things worth mentioning:
- This is just a minimal example to illustrate the issue and is not the same as my production code
- The
.csprojspecifies<TargetFramework>net9.0</TargetFramework>and<Nullable>enable</Nullable> - Type
Tshould be able to be either a reference or a value type
MyComponent.razor:
@typeparam T<div>@ToValueFunc(Text)</div>@code { [Parameter] public string? Text { get; set; } [Parameter, EditorRequired] public Func<string?, T?> ToValueFunc { get; set; } = default!;}MyComponentTest.razor:
@page "/mycomponenttest"<MyComponent T="int" ToValueFunc="@DivideByFour" Text="@_text" />@code { private string? _text = "4"; private int? DivideByFour(string? text) => int.TryParse(text, out int value) ? value / 4 : null;}Why is this happening and is there any way to get it to obey the nullability specified in MyComponent.razor?