Skip to content

@null attribute value results in ambiguity #10615

Open
@jjonescz

Description

@jjonescz

In a .razor component:

<div style="@null">x</div> <!-- error -->
<div style="@((string?)null)">x</div> <!-- ok -->

error CS0121: The call is ambiguous between the following methods or properties: 'RenderTreeBuilder.AddAttribute(int, string, string?)' and 'RenderTreeBuilder.AddAttribute(int, string, MulticastDelegate?)'

Note that passing null to an attribute is a supported scenario: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-7.0#conditional-attribute-rendering. Although perhaps it doesn't make much sense to pass a literal @null but rather via some variable.

It also works fine in .cshtml.

Related:

I think compiler cannot do much here, we don't analyze the user-written C# code. At most we could offer an analyzer to fix this by casting.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions