C # enums: can they accept elements and functions like java enums?

in java, you can specify enum as a constructor, as well as member variables and functions.

I was wondering if this is possible in C # enums. if so, how?

Many thanks!

+3
source share
9 answers

The only way to do something similar is to use extension methods, which can make it look as if the enumeration had member methods.

In addition, you can create an accompanying structure type for your enumeration, which has a property for the enumerated value, and then adds additional properties and methods to support this value.

+4

. Java , # - , .

+1

- . , . # , , , . . .

- . , , , .

- , , , , , , , , . :

+1

Java TypeSafe ( Java , enum Java 5 ):

. 21 (, PDF) .

, , , # ( Java 5).

+1

, , - Description , 3

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

, / , . , , enum.

, - , , , , /, .

+1

,

0

#. /.

0

As far as I know, in C # you cannot. Although why do you need it too? Seems too weird thing for attaching variables and functions too!

0
source

You can define extension methods for enumeration types, but you cannot add state to enums, since enumerations are represented as simple integer types inside, and there is nowhere to store the state.

0
source

Source: https://habr.com/ru/post/1717660/


All Articles