Další skvělá feature C# 6.0, žádné hardcoded řetězce:
using Stuff = Some.Cool.Functionality class C { static int Method1 (string x, int y) {} static int Method1 (string x, string y) {} int Method2 (int z) {} string f<T>() => nameof(T); } var c = new C() nameof(C) -> "C" nameof(C.Method1) -> "Method1" nameof(C.Method2) -> "Method2" nameof(c.Method1) -> "Method1" nameof(c.Method2) -> "Method2" nameof(z) -> "z" // inside of Method2 ok, inside Method1 is a compiler error nameof(Stuff) = "Stuff" nameof(T) -> "T" // works inside of method but not in attributes on the method nameof(f) -> "f" nameof(f<T>) -> syntax error nameof(f<>) -> syntax error nameof(Method2()) -> error "This expression does not have a name"
Zdroj: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/nameof