Here is my code right now. But I would like to move those "Add" out from the constructor. Can we initialize Dictionary
when we new
it? or you have another better idea. Basically I want to define few characters which are used in many places.
public class User
{
public enum actionEnum
{
In,
Out,
Fail
}
public static Dictionary<actionEnum, String> loginAction = new Dictionary<actionEnum, string>();
public User()
{
loginAction.Add(actionEnum.In, "I");
loginAction.Add(actionEnum.Out, "O");
loginAction.Add(actionEnum.Fail, "F");
}
.....
}
You can use C# 3's collection initializer syntax:
public static Dictionary<actionEnum, String> loginAction = new Dictionary<actionEnum, string> {
{ actionEnum.In, "I" },
{ actionEnum.Out, "O" },
{ actionEnum.Fail, "F" }
};
Note, by the way, that the dictionary is mutable; any code can add or remove values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With