Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the use-case for specifying the underlying type in enums?

Tags:

c#

types

enums

What is the point of having

enum SomeEnum : byte // <----
{
  SomeValue = 0x01,
  ...
}

when you have to make a cast just to assign it to the same type of variable as the enums underlying type?

byte b = (byte)SomeEnum.SomeValue;
like image 912
Marlon Avatar asked Apr 16 '10 04:04

Marlon


People also ask

What is the underlying type of an enum declaration that does not explicitly define an underlying type?

An enum declaration that does not explicitly declare an underlying type has an underlying type of int .

Is enum value type or reference?

Enum is a reference type, but any specific enum type is a value type. In the same way, System. ValueType is a reference type, but all types inheriting from it (other than System. Enum ) are value types.

What are the parts of an enum called?

In computer programming, an enumerated type (also called enumeration, enum, or factor in the R programming language, and a categorical variable in statistics) is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type.

What type is an enum value C#?

In the C# language, enum (also called enumeration) is a user-defined value type used to represent a list of named integer constants. It is created using the enum keyword inside a class, structure, or namespace. It improves a program's readability, maintainability and reduces complexity.


3 Answers

Not much point, really, except that if the default underlying type (int) is not enough for you, ie. you want to use higher integer values than that then you can make it long. This can be useful when you have a [Flags] enum with more than 32 values.

You can make it byte or short just to restrict the range of values, but it will actually still take 4 bytes (ie. same as int).

like image 141
EMP Avatar answered Sep 28 '22 07:09

EMP


From enum (C# Reference)

The underlying type specifies how much storage is allocated for each enumerator. However, an explicit cast is needed to convert from enum type to an integral type.

like image 34
Adriaan Stander Avatar answered Sep 28 '22 07:09

Adriaan Stander


Apart from the technical reasons why... There is a design principle here.

specifying the enum as having byte storage is an implementation detail. Using the enum is a different issue, you should not have to know or care about the implementation details of it.

In client code, the fact that you are using an enum should mean that you are in fact meaning to use an enum, not a byte, or long etc. Otherwise why not just use the datatype you mean.

Strongly type languaged such as C# strive to make it just a little harder to step outside your coding "contracts" this usually helps make app design just that little bit better.

Now of course I am not saying that there are not times that you have to get involved in implementation details, a good example is in say an object relational mapper (ORM) where you are mapping a C# datatype to a database datatype, enums are a good example where you have to then know its storage type to map it. But in these cases, its IMO good to have to explicity cast or reflect, its a good flag in reviews that here you are specifically stepping outside the usual usage.

like image 37
Tim Jarvis Avatar answered Sep 28 '22 07:09

Tim Jarvis