Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't we add static methods to enums? [duplicate]

I wonder why we can't add static methods (only methods, not properties) into enums? Is there any explanation for that?

It would be very useful if it was allowed.

And I also want to learn who forbids us to do it? Is it IL or C#?

Edit:

I don't want to use extension methods. Because I dont need to pass an instance of that enum. I don't need it's value there...

I want to call something like FooTypes.GetGoodFoos() not something FooTypes.BadFoo.GetSomething()

Edit 2:

Is that only me who thinks this could be more useful rather than writing this method in another class?

public enum Colors
{
    Red,
    LightRed,
    Pink,
    /* .
       .
       . */
    Green

    public static Colors[] GetRedLikes()
    {
        return new Colors[]
        {
            Colors.Red,
            Colors.LightRed,
            Colors.Pink
        }
    }
}
like image 713
Yves Avatar asked Sep 23 '14 10:09

Yves


People also ask

Can an enum have static methods?

The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.

Is enum always static?

As enums are inherently static , there is no need and makes no difference when using static-keyword in enums . If an enum is a member of a class, it is implicitly static.

Can you add methods to enums?

You can use extension methods to add functionality specific to a particular enum type.

Are enums static by default?

Yes, enums are effectively static.


2 Answers

As the other answers say, it's not possible.

I know, this does not answer your question, but I want to give an alternative to your example. Because, basically what you try to archive is already possible with flags. So let me take your "GetRedLikes" example:

[Flags]
public enum Colors : byte
{
    Transparent = 0,                                         // = 0 (00000000)
    Red         = 1 << 0,                                    // = 1 (00000001)
    LightRed    = 1 << 1,                                    // = 2 (00000010)
    Pink        = 1 << 2,                                    // = 4 (00000100)
    Green       = 1 << 3,                                    // = 8 (00001000)

    RedLikes    = Colors.Red | Colors.LightRed | Colors.Pink // = 7 (00000111)
}

Then Colors.RedLikes will contain Red, LightRed and Pink. All the magic is done by bits, as always. Your condition then should look like this:

Colors c = Colors.LightRed;
if(c & Colors.RedLikes != 0)
{
    // c is red-alike
}

Of course, this solution will not allow you to do very complex algorithms, it's no method type replacement. But it allows you to combine more than one enum in a set. If you need further functions, you have to build a method in an extra class.

like image 167
Martin Braun Avatar answered Oct 23 '22 12:10

Martin Braun


I use static class for same cases:

 public enum SomeEnum
{
    Item1,
    Item2,
    Item3
}

public static class SomeEnumHelper
{
    public static SomeEnum[] GetMainItems() 
    {
        return new[] {SomeEnum.Item1, SomeEnum.Item2};
    }
}
like image 22
razon Avatar answered Oct 23 '22 14:10

razon