enum is a set (list) of constant .
Each enumeration has an integer type associated with it, except char
, the default type is int
.
Allowed types are byte
, sbyte
, short
, ushort
, int
, uint
, long
or ulong
.
The type is assigned to the enumeration as follows:
public enum : long Command
{
}
The first value of the enumeration receives the value 0
, the value is then incremented by 1 and assigned to the next constant, the process is repeated for all constants.
Picking up your example
public enum Command
{
Exit,
SendMessage,
WhoIs
}
Each constant receives the following values:
Exit = 0
, SendMessage = 1
and WhoIs = 2
Default values can be overridden using initializers :
public enum Command
{
Exit = 1,
SendMessage,
WhoIs = 4
}
Initializers , in addition to assigning a value to the constant, initialize the sequence of values of the following constants:
Exit = 1
, SendMessage = 2
and WhoIs = 4
Having said that and answering your question the values of the constants of an enum can only be integer type.
However, by using Attributes , we can do what you want.
/ p>
First we write a class to represent this attribute, this class inherits from Attribute :
public class StringValueAttribute : Attribute
{
//Propriedade que recebe a string que irá ser atribuída à constante
public string StringValue { get; protected set; }
//No construtor inicializa-se a propriedade
public StringValueAttribute(string value)
{
StringValue = value;
}
}
To get the value of the attribute we refer to Reflection .
To make things easier we'll create the code as an extension method.
public static class ExtensionMethods
{
public static string GetStringValue(this Enum value)
{
var type = value.GetType();
FieldInfo fieldInfo = type.GetField(value.ToString());
var attributes = fieldInfo.GetCustomAttributes(
typeof(StringValueAttribute), false) as StringValueAttribute[];
var stringvalue = attributes != null && attributes.Length > 0 ? attributes[0].StringValue : value.ToString();
return stringvalue;
}
}
How to use
Attributes are first associated with elements of enum
public enum Command
{
[StringValue("/exit")]
Exit,
[StringValue("/msg")]
SendMessage,
[StringValue("/whois")]
WhoIs
}
Then, to get the string, the GetStringValue()
method is used.
You can not use switch
so use else if
:
if(cmd == Command.Exit.GetStringValue())
{
......
......
}
else if(cmd == Command.SendMessage.GetStringValue())
{
......
......
}
else if(cmd == Command.WhoIs.GetStringValue())
{
......
......
}
....
.....
Note:
The GetStringValue class was written / adapted using the existing information in net . Unfortunately I do not remember where.