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.razor
ToValueFunc
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
?
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.razor
ToValueFunc
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
?
1 Answer
Reset to default 2Your Func
specifies a nullable T?
, so your MyComponent
declaration should be:
<MyComponent T="int?" ToValueFunc="@DivideByFour" Text="@_text" />
Or let the compiler sort it out with:
<MyComponent ToValueFunc="@DivideByFour" Text="@_text" />
You should also probably null check ToValueFunc
like this in MyComponent
:
@typeparam T
<div>@(Text is null ? "Not Set" : ToValueFunc(Text))</div>
@code {
[Parameter] public string? Text { get; set; }
[Parameter, EditorRequired] public Func<string?, T?> ToValueFunc { get; set; } = default!;
protected override void OnInitialized()
{
ArgumentNullException.ThrowIfNull(ToValueFunc, nameof(ToValueFunc));
}
}