Which is the better way to avoid magic string keys? Using string const keys in a class or using enumeration?

Posted by user596314 on Stack Overflow See other posts from Stack Overflow or by user596314
Published on 2011-02-14T07:16:01Z Indexed on 2011/02/14 7:25 UTC
Read the original article Hit count: 153

Filed under:
|

My idea is to avoid magic string keys in my Asp.Net MVC application. To do so, I want to create string constant keys to be shared in the application.

For example, I can write TempData[MyClass.Message] or TempData[MyEnum.Message.ToString()] instead of TempData["Message"].

public class MyClass
{
    public const string Message = "Message";
}

and

public enum MyEnum
{
   Message,
   Others
}

My questions are: Which is the better way to avoid magic string keys? Using string const keys in a class or using enumeration together with ToString()?

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc