Aspnetcore.docs: C# and razor do NOT support overloaded local functions

Created on 28 May 2020  Â·  6Comments  Â·  Source: dotnet/AspNetCore.Docs

[EDIT by guadrex to add the metadata. @Seabizkit, please use the This Page feedback button+form at the bottom of any topic to open an issue. It will automatically add the metadata and ping the author.]

overloads do not work

simple example

@{

    void RenderName(string name)
    {
        <p>Name: <strong>@name</strong></p>
    }

    void RenderName(int intValue)
    {
        <p>Name: <strong>@intValue</strong></p>
    }
}

Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Source - Docs.ms doc-enhancement product-question

Most helpful comment

If we close as by-design, it can be labelled with the product-question label .............

I guess this makes sense _not_ to cover it. The error message is fairly clear in the sense that 'you can't do this.'

All 6 comments

@NTaylorMullen Is this supported? It's not addressed in the Razor syntax topic.

@NTaylorMullen Is this supported? It's not addressed in the Razor syntax topic.

Overloads should work. @Seabizkit what issue are you seeing with them?

@NTaylorMullen

that complains... as is, does not compile....."name already exist... compile error" but what is interesting unless i missed something... but below works... surely they should both work.

@functions 
{
     void RenderName(string name)
    {
        <p>Name: <strong>@name</strong></p>
    }

    void RenderName(int intValue)
    {
        <p>Name: <strong>@intValue</strong></p>
    }
}

key word functions, makes it work... how odd

key word functions, makes it work... how odd

Ahh, interesting what you're seeing then is a C# restriction.When you say @functions {...} what you're saying is add the following to my class body where @{} entails add the following to a method in my class body (a local function). Therefore since C# doesn't support overloaded local functions you can't utilize them in @{}.

I can reproduce in a simple console app:
image

@guardrex is there a "by design" equivalent label?

No, but we don't cover it in the Razor syntax topic. Would you like it covered there?

If we close as by-design, it can be labelled with the product-question label .............

I guess this makes sense _not_ to cover it. The error message is fairly clear in the sense that 'you can't do this.'

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Mattacks picture Mattacks  Â·  3Comments

sonichanxiao picture sonichanxiao  Â·  3Comments

Rick-Anderson picture Rick-Anderson  Â·  3Comments

royshouvik picture royshouvik  Â·  3Comments

wgutierrezr picture wgutierrezr  Â·  3Comments