Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor component with func parameter that has nullable generic type parameters

$
0
0

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 .csproj specifies <TargetFramework>net9.0</TargetFramework> and <Nullable>enable</Nullable>
  • Type T should 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?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>