CallerMemberName
and local methods
#6486
-
Why the compiler inserts the parent method identifier instead of the local method indentifier here ? static class Program
{
static void Main()
{
Print(); // will print Main
Level(); // will also print Main
static void Level()
=> Print();
static void Print([CallerMemberName]string name = null!)
=> Console.WriteLine(name);
}
} Of course, it works the same with top-level program, but will print |
Beta Was this translation helpful? Give feedback.
Answered by
RikkiGibson
Sep 24, 2022
Replies: 1 comment 3 replies
-
I think the idea here is that local functions are not members and thus their names don't get used for CallerMemberName. Only the containing member's name gets used, i.e., Main. Since the containing member will always be the same for all calls of a local function, the current behavior is perhaps not that useful. |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
FaustVX
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think the idea here is that local functions are not members and thus their names don't get used for CallerMemberName. Only the containing member's name gets used, i.e., Main.
Since the containing member will always be the same for all calls of a local function, the current behavior is perhaps not that useful.