Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for top level statements #980

Draft
wants to merge 7 commits into
base: draft-v9
Choose a base branch
from

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)
    {
    }
}

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.

2 participants