Skip to content

[Analyzer proposal]: Replace literal enum member ToString with nameof #106381

Open
@drewnoakes

Description

@drewnoakes

sharplab

static class C
{
    enum E { A }
    static string M1() => nameof(E.A);
    static string M2() => E.A.ToString();
}
; Core CLR 8.0.724.31311 on x86

C.M1()
    L0000: mov eax, [0x1766db7c]
    L0005: ret

C.M2()
    L0000: mov ecx, 0x2f62c9d0
    L0005: call 0x06b6300c
    L000a: xor ecx, ecx
    L000c: mov [eax+4], ecx
    L000f: mov ecx, eax
    L0011: call dword ptr [0x7046b8c]
    L0017: ret

Potential complications around flags enums.

These are not exactly functionally equivalent as they return different object references.

ReferenceEquals(C.M1(), C.M2()) // false

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions