Question:
How do you handle enums in c#?Answer:
To start with, you might check out the Enums article in msdn.Enumerated types are a good way to represent that a value can be a choice of a set of named constants or a multiple-choice of those values in the case of bit flags.
Enums are based on Int32 by default and can be based on integral types (byte, int, long etc) in general.
If you don't give explicit values the compiler will give 0 to the first enumerator and then increment by 1.
Each occurance of an enum typed variable in code will be translated into a numeric literal.
Define an Enum implicitly.
public enum Vote { None, Good, Medium, Bad, }
Define an Enum using explicit type
public enum Vote : byte { None, Good, Medium, Bad, }
Define an Enum explicit values
public enum Vote : byte { None = 0, Good = 1, Medium = 2, Bad = 3, }
Compiler assigns zero as default value
private Vote myVote;The compiler will initialize the value of myVote to (Vote)0.
That gets you into trouble if you excluded zero as a choice of your enum.
You should always have one enum choice that equals 0.
Transform enum to string.
string s = myVote.ToString();
Transform string to enum.
string str = "Good"; Vote myVote = (Vote)Enum.Parse(typeof(Vote), str); // this will throw an exception if the parse fails
Try to transform string to enum.
string str = "Good"; Vote myVote; Boolean parseOk = Enum.TryParse<Vote>(str, out myVote);
Test if value is defined on enum.
string str = "Good"; Enum.IsDefined(typeof(Vote), str);
Define Flaged enum
[Flags] public enum Vote : byte { None = 0x0, // '0x' is the marker for hex notation Good = 0x1, Medium = 0x2, Bad = 0x4, Respectable = 0x3 }
Manipulating Flaged enums
// combining enumns with bitwise OR Vote vote = Vote.Good; vote |= Vote.Medium; Console.WriteLine("vote = " + vote); // vote = Respectable // substracting enumns with bitwise XOR vote = vote ^ Vote.Good; Console.WriteLine("vote = " + vote); // vote = Medium // testing enums with bitwise AND Vote strangeVote = Vote.Good | Vote.Bad; Boolean isGood = (strangeVote & Vote.Good) == Vote.Good; Console.WriteLine("isGood = " + isGood); // isGood = True // test equality vote = (Vote)3; Boolean isRespectable = vote == Vote.Respectable; Console.WriteLine("isRespectable = " + isRespectable); // isRespectable = True // assigning standard vote = Vote.Good; Console.WriteLine("vote = " + vote ); // vote = Good // assigning combined enum as integral vote = (Vote)7; Console.WriteLine("vote = " + vote ); // vote = Respectable, Bad // assigning value outside range vote = (Vote)0xF; Console.WriteLine("vote = " + vote ); // vote = 15 // there is no enum value for 15 !!!
Two ways of testing for flags on enum
Some people find it strange to use a bitwise AND so the dot.net team added a method HasFlag()
// The first way to test a flag is bitwise AND Vote strangeVote = Vote.Good | Vote.Bad; Boolean isGood = (strangeVote & Vote.Good) == Vote.Good; Console.WriteLine("isGood = " + isGood); // isGood = True // The second way using HasFlag() isGood = strangeVote.HasFlag(Vote.Good); Console.WriteLine("isGood = " + isGood); // isGood = True
happy programming ...
No comments:
Post a Comment