The nameof Operator · TL;DR
1 min readTL;DR
nameof(x) (C# 6) returns the simple, unqualified name of a variable, type, member or namespace as a compile-time constant string: nameof(order.Total) is "Total", nameof(List<int>) is "List". The expression is never evaluated, so there is no runtime cost and no null risk. Its purpose is to replace "magic strings" that break silently on rename: argument exceptions, INotifyPropertyChanged, logging, attribute arguments, EF Core and MVC helpers. Because it is a constant it works in const fields, switch cases and attribute arguments. Traps: it gives the simple name, not the fully qualified one (use typeof(T).FullName for that); nameof(OrdersController) is "OrdersController", not the route name "Orders"; and it gives the C# name, not a JSON or database name changed by attributes.