clamp
clamp

Reputation: 34006

c# enums: can they take members and functions like java enums?

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!

Upvotes: 3

Views: 2462

Answers (9)

Chris Marisic
Chris Marisic

Reputation: 33098

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.

Upvotes: 1

Yishai
Yishai

Reputation: 91881

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).

Upvotes: 1

eKek0
eKek0

Reputation: 23289

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:

Upvotes: 1

sepp2k
sepp2k

Reputation: 370132

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.

Upvotes: 0

48klocs
48klocs

Reputation: 6103

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

Upvotes: 1

Siyfion
Siyfion

Reputation: 416

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!

Upvotes: 0

JaredPar
JaredPar

Reputation: 754715

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

Upvotes: 0

JDunkerley
JDunkerley

Reputation: 12495

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

Upvotes: 0

Jeff Yates
Jeff Yates

Reputation: 62377

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.

Upvotes: 4

Related Questions