Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use int in enum declaration?

Tags:

c#

enums

What's the point of using : int in the enum declaration as following?

public enum AAType : int
{
    Folder = 0,    
    File = 1,   
    Link = 2
}
like image 972
Carlos Liu Avatar asked Apr 12 '10 01:04

Carlos Liu


People also ask

Should I use enum or int?

There are a very small number of places where using int flags is still preferable in modern Java code, but in most cases you should prefer to use an enum , due to the type safety and expressiveness that they offer.

Can we use integer in enum?

No, we can have only strings as elements in an enumeration.

Is enum always int?

Enumerations are integers, except when they're not - Embedded.com.

What are the advantages of using an enum over an int?

The advantages of using enums are that they are very easy to use and represented as strings but processed as integers. Enums are easy to maintain and improve code readability because they provide symbolic named constants, which means you need to remember the names, not the integer values.


1 Answers

The default underlying type of an enum is int, so by specifying it explicitly you only (perhaps) gain in clarity, but the behavior's just the same as if : int was omitted.

like image 51
Alex Martelli Avatar answered Oct 13 '22 22:10

Alex Martelli