Why should I make the underlying type of an Enum Int32 instead of byte?
.net, c#, enums
Solution
Have a look on MSDN for the reason.
Here is an excerpt:
An enumeration is a value type that defines a set of related named constants. By default, the System.Int32 data type is used to store the constant value. Even though you can change this underlying type, it is not necessary or recommended for most scenarios. Note that no significant performance gain is achieved by using a data type that is smaller than Int32. If you cannot use the default data type, you should use one of the Common Language System (CLS)-compliant integral types, Byte, Int16, Int32, or Int64 to make sure that all values of the enumeration can be represented in CLS-compliant programming languages.
Problem
Given the following enum: ``` public enum Operations_PerHourType : byte { Holes = 1, Pieces = 2, Sheets = 3, Strips = 4, Studs = 5 } ``` When I run the Microsoft code analysis tool, it tells me: CA1028 : Microsoft.Design : If possible, make the underlying type of 'Enums.Operations_PerHourType' System.Int32 instead of 'byte'. It will never have more than a couple possible values, so I declared it as a byte. Why would they recommend using int32? More values for future scalability? Or is there a performance improvement?