c# - nameof() operator for static string -


i understand use of nameof() operator exception handling, logging, etc. not understand example below coming directly microsoft code.

public static class sessionkeys {     public static class login     {                    public static string accesstoken = nameof(accesstoken);          public static string userinfo = nameof(userinfo);     } } 

how more useful

public static class sessionkeys {     public static class login     {         public static string accesstoken = "accesstoken";         public static string userinfo = "userinfo";     } } 

nameof operator evaluated @ compile time, once application compiled there no difference between 2 solutions.

however, using nameof in case has few benefits:

  • it makes string value less “magic”. instead of being disconnected magic string, semantic reasoning behind value clear: it’s name of variable itself.
  • the name actual reference name, both connected. allows refactor either of them , automatically affect other one. makes “string” appear reference when looking references variable. know has been used.

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -