public static class ErrorCode
{
public const IDictionary<string, string> ErrorCodeDic = new Dictionary<string, string>()
{
{ "1", "User name or password problem" }
};
}
public static class ErrorCode
{
public const IDictionary<string , string > m_ErrorCodeDic;
public static ErrorCode()
{
m_ErrorCodeDic = new Dictionary<string, string>()
{ {"1","User name or password problem"} };
}
}
You can use the static/class constructor to initialize your dictionary:
public static class ErrorCode
{
public const IDictionary<string, string> ErrorCodeDic;
public static ErrorCode()
{
ErrorCodeDic = new Dictionary<string, string>()
{ {"1", "User name or password problem"} };
}
}
Make the Dictionary a static, and never add to it outside of your static object's ctor. That seems to be a simpler solution than fiddling with the static/const rules in C#.
The correct syntax ( as tested in VS 2008 SP1), is this:
public static class ErrorCode
{
public static IDictionary<string, string> ErrorCodeDic;
static ErrorCode()
{
ErrorCodeDic = new Dictionary<string, string>()
{ {"1", "User name or password problem"} };
}
}
If you want to dictionary items to be readonly (not just the reference but also the items in the collection) then you will have to create a readonly dictionary class that implements IDictionary.
Check out ReadOnlyCollection for reference.
BTW const can only be used when declaring scalar values inline.
The problem with your initial example was primarily due to the use of const rather than static; you can't create a non-null const reference in C#.
I believe this would also have worked:
public static class ErrorCode
{
public static IDictionary<string, string> ErrorCodeDic
= new Dictionary<string, string>()
{ {"1", "User name or password problem"} };
}
Also, as Y Low points out, adding readonly is a good idea as well, and none of the modifiers discussed here will prevent the dictionary itself from being modified.
Edit: Per Chris's comment below, using Dictionary<string, string> over StringDictionary is generally preferred but will depend on your situation. If you're dealing with an older code base, you might be limited to the StringDictionary. Also, note that the following line:
myDict["foo"]
will return null if myDict is a StringDictionary, but an exception will be thrown in case of Dictionary<string, string>. See the SO post he mentioned for more information, which is the source of this edit.
Use a public static property dictionary and wrap the static private dictionary inside.
Then you only need to take care about mutable or immutable types, first one you need to iterate inside the wrapper. That allows you to read a dictionary, but not edit it (not the entries and not the whole reference) with the option to allow edit inside the set{} part using any authentication model you prefer.
(I was looking for something different, like static performance in parallel coding, saw this search result and find the wrapper approach missing.)
For those who don't know what wrapping is, here a non static example (you can easily add the static key-word):
public Dictionary<string, Boolean> Access
{
get
{
// Same here, iterate if needed..
return new Dictionary<string, Boolean>(prv_Access);
}
set
{
MainWindow.NewSession.Renew();
if (MainWindow.NewSession.Actual)
{
prv_HistoryAccess.Add(DateTime.Now, new Dictionary<string, Boolean>(prv_Access));
// Here you would need to iterate when you deal with mutables..
prv_Access = value;
}
}
}