Skip to content

Add support for top level statements#980

Closed
RexJaeschke wants to merge 10 commits intodotnet:draft-v9from
RexJaeschke:Add-support-for-top-level-statements
Closed

Add support for top level statements#980
RexJaeschke wants to merge 10 commits intodotnet:draft-v9from
RexJaeschke:Add-support-for-top-level-statements

Conversation

@RexJaeschke
Copy link
Contributor

No description provided.

@RexJaeschke RexJaeschke added the type: feature This issue describes a new feature label Nov 1, 2023
@RexJaeschke RexJaeschke added this to the C# 9.0 milestone Nov 1, 2023
@RexJaeschke RexJaeschke marked this pull request as draft November 1, 2023 13:15
}
```

The class name `Program` shall be referenceable by name from within the application. However, the method name `«Main»` is used here for illustrative purposes only. The actual name generated by the implementation is unspecified, and cannot be referenced by name from within the application.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this say that the generated name is readable via CallerMemberNameAttribute?

System.Console.WriteLine(WhoAmI()); // outputs "<Main>$"

System.Action action = () => System.Console.WriteLine(WhoAmI());
action(); // outputs "<Main>$" again, not the name of the method generated for the lambda

string WhoAmI(
    [System.Runtime.CompilerServices.CallerMemberName]string member = null)
{
    return member;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KalleOlaviNiemitalo, I confirm that the generated name can be found, but it's not clear to me how one might make use of that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be best to add something about this to the specification of CallerMemberNameAttribute, to clarify that the name of the generated method is substituted even though it does not occur in the source code. Because this doesn't work like in lambda expressions where the user-specified name is used even though the code is generated to a differently-named method.

using System;
using System.Runtime.CompilerServices;

class C
{
    void M(int i)
    {
        // calls N(i, "M") even though the call is in a method named something like <M>b__0
        Action a = () => N(i);
    }
    
    static void N(int i, [CallerMemberName] string mem = null)
    {
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the end of line 73, I added the following sentence:

It is, however, available via the attribute System.Runtime.CompilerServices.CallerMemberName.

Also, in that attribute's section, I added

(In the case of a function invocation from a top-level statement (§top-level-statements), the member name is that generated by the implementation.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KalleOlaviNiemitalo Do these changes resolve this conversation?

@BillWagner BillWagner force-pushed the Add-support-for-top-level-statements branch from 6e3e2f4 to 2dcda8f Compare November 6, 2025 19:26
@BillWagner
Copy link
Member

Closed in favor of #1454

@BillWagner BillWagner closed this Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: feature This issue describes a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants