tags:

views:

224

answers:

9

hello, in java it is possible to give an enum a constructor as well as member variables and functions.

i was wondering if something like this is possible in c# enums as well. if so, how?

thanks a lot!

+3  A: 

The only way to do something similar to this is to use extension methods, which can make it appear as though the enumeration has member methods.

Other than that, you could create a companion struct type to your enumeration that has a property for the enumeration value and then adds additional properties and methods to support that value.

Jeff Yates
A: 

Not directly but you can use Extension methods to provide similar functionality

JDunkerley
A: 

This is not possible in C#. Enums can only have name / value members.

JaredPar
A: 

As far as I am aware, no you can't in C#. Although why would you want too? Seems a bit of an odd thing to attach variables and functions too!

Siyfion
You'd want it to do proper OO programming - put behaviour related to the enum with the enum itself rather than having switch clauses all over the place.
Michael Borgwardt
because i do this a lot in java. it is actually one of my favourite features ;) for example you have an enum height and in each one you have an additional final member that contains the actual height. SMALL.height = 80, NORMAL.height = 100, BIG.height = 120
clamp
I guess it depends what you want your enum to do, but I can't ever imagine needing mine to do that. As for it being OO, normally I have a class that wraps an enum to provide any OO-ism I need.
Siyfion
+1  A: 

It is not. In Java, enumerations are a class while in C#, an enumeration is just syntactic sugar wrapping a primitive type.

48klocs
A: 

You can define extension methods for enum types, but you can't add state to enums, since enums are represented as simple integer types internally, and there'd be nowhere to store the state.

sepp2k
+1  A: 

Enums are strongly typed constants. They are essentially unique types that allow you to assign symbolic names to integral values. In the C# tradition, they are strongly typed, meaning that an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same. Along the same lines, integral types and enums are not implicitly interchangable. All assignments between different enum types and integral types require an explicit cast.

You can't use member variables or constructors in an enum. Maybe what you are looking for is an struct.

A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types. The declaration of a struct takes the following form:

eKek0
+1  A: 

You could imitate the Java TypeSafe enum pattern (what was so common in Java before enum was introduced in Java 5 to address it):

See Item 21 here (warning, PDF link) for a description.

You would do this if the object functionality was more important than the switch functionality, since in C# you can get the type saftey without it (which you couldn't in Java before 5).

Yishai
+1  A: 

One thing I've always loved to do is to use the Description attribute on my enums so I can store 3 values of my enum easily

Public Enum States
{
    [Description("Florida")]
    FL = 124
}

And then I had a class that easily reads to/from the description attribute so I could store a whole database code table in an enum file. Aside from all the posters bringing up extension methods you could use attributes to drive log with your enum classes.

You would still need to leverage another class to actually do something with the enum but you could use attributes to add more depth to your enum instead of just having the key/value pair that it basically is.

Chris Marisic