3

Can I overload the nameof operator in C#?

The C# programming guide is outdated and the C# 6 under the hood doesn't help me.

How can I overload the nameof operator?

cSteusloff
  • 2,487
  • 7
  • 30
  • 51
  • 3
    `nameof` is evaluated at compile time, so concept of overloading it doesn't make much sense to me – Evk Dec 14 '17 at 10:58

1 Answers1

6

The nameof operator is evaluated at compile-time.

The nameof expression is a constant. In all cases, nameof(...) is evaluated at compile-time to produce a string. Its argument is not evaluated at runtime, and is considered unreachable code (however it does not emit an "unreachable code" warning).

So, these operator cannot be overloaded.

Solid-Fan
  • 118
  • 8
  • 1
    What are you quoting? It make sense to add reference (name, link) whenever using quote. – Sinatr Dec 14 '17 at 14:32
  • I don't know where @solid-fan is quoting from but here is the Microsoft [documentation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/operator-overloading#overloadable-operators). – petrosmm Oct 24 '19 at 19:14