I\'ve found this all over the place in this code:
public enum Blah: int
{
blah = 0,
blahblah = 1
}
Why would it need to inherit fro
it gives it a numeric value, that is all, as far as i know
You don't need to, it's implied. According to MSDN:
An enumeration is a set of named constants whose underlying type is any integral type except Char. If no underlying type is explicitly declared, Int32 is used. Enum is the base class for all enumerations in the .NET Framework.
This means you could usebyte
, sbyte
, ushort
, int
, uint
, long
, or ulong
.
Also, setting the values the way you have described (blah=0, blahblah=1), while redundant, is OK, since, according to the C# Specification
If the declaration of the enum member has no initializer, its associated value is set implicitly, as follows:
• If the enum member is the first enum member declared in the enum type, its associated value is zero.
• Otherwise, the associated value of the enum member is obtained by increasing the associated value of the textually preceding enum member by one. This increased value must be within the range of values that can be represented by the underlying type, otherwise a compile-time error occurs.
int
is by default the type of any enum
. It does not need to be declared explicitly.
It's more useful when you want to use something else (byte
, long
, and friends).
An enum "member" can have an underlying "value". The inheritance from int tells you what type the value will take.
According to the documentation:
Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of the enumeration elements is int.
So, no, you don't need to use int. It would work with any integral type. If you don't specify any it would use int as default and it's this type that will be used to store the enumeration into memory.
You do not need to inherit as the base type of an Enum is by default, int.
http://msdn.microsoft.com/en-us/library/sbbt4032(v=vs.71).aspx
base-type (Optional)
The underlying type that specifies the storage allocated for each enumerator. It can be one of the integral types except char. The default is int.