Skip to content

Commit d1f4333

Browse files
RexJaeschkeBillWagner
authored andcommitted
restore example (#1548)
1 parent 6867b56 commit d1f4333

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

standard/attributes.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,38 @@ For invocations that occur within field or event initializers, the member name u
842842
843843
For invocations that occur within declarations of instance constructors, static constructors, finalizers and operators the member name used is implementation-dependent.
844844
845-
For an invocation that occurs within a local function, the name of the member method that calls that local function is used. Consider the following: if member method `M` calls local function `F1`, which in turn calls local function `F2`, and `F2` has a parameter marked with this attribute, the method name passed to `F2` is `M`, because a local function is *not* a function member!
845+
> *Example*: Consider the following:
846+
>
847+
> <!-- Example: {template:"standalone-console", name:"CallerMemberName1", inferOutput:true} -->
848+
> ```csharp
849+
> class Program
850+
> {
851+
> static void Main()
852+
> {
853+
> F1();
854+
>
855+
> void F1([CallerMemberName] string? name = null)
856+
> {
857+
> Console.WriteLine($"F1 MemberName: |{name}|");
858+
> F2();
859+
> }
860+
>
861+
> static void F2([CallerMemberName] string? name = null)
862+
> {
863+
> Console.WriteLine($"F2 MemberName: |{name}|");
864+
> }
865+
> }
866+
> }
867+
> ```
868+
>
869+
> which produces the output
870+
>
871+
> ```console
872+
> F1 MemberName: |Main|
873+
> F2 MemberName: |Main|
874+
> ```
875+
>
876+
> This attribute supplies the name of the calling function member, which for local function `F1` is the method `Main`. And even though `F2` is called by `F1`, a local function is *not* a function member, so the reported caller of `F2` is also `Main`. *end example*
846877
847878
### 23.5.7 Code analysis attributes
848879

0 commit comments

Comments
 (0)