Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Allow methods in enums #1863

Closed
TheUnlocked opened this issue Sep 17, 2018 · 2 comments
Closed

Proposal: Allow methods in enums #1863

TheUnlocked opened this issue Sep 17, 2018 · 2 comments

Comments

@TheUnlocked
Copy link

Currently, it's not possible to define a method inside of an enum. This, for example, or anything like it does not compile:

public enum FooType{
  FOO,
  BAR,
  BANG,
  POW;

  public string GetName(){
    switch(this){
      case FooType.FOO:
        return "Foo";
      case FooType.BAR:
        return "Foo";
      case FooType.BANG:
        return "Foo";
      case FooType.POW:
        return "Foo";
    }
    return null;
  }
}

It is possible to emulate this effect using extensions:

public enum FooType{
  FOO,
  BAR,
  BANG,
  POW
}

public static class FooTypeExtension{
  public static string GetName(this FooType ftype){
    switch(ftype){
      case FooType.FOO:
        return "Foo";
      case FooType.BAR:
        return "Foo";
      case FooType.BANG:
        return "Foo";
      case FooType.POW:
        return "Foo";
    }
    return null;
  }
}

However, the fact that you're required to write a whole other class just to add a method to the enum seems a little big ridiculous to me. I'm not going to go as far as to suggest that an enum should be a full-fledged class (that has already been suggested in #274), but the capacity to do things which are already legal through extensions seems reasonable.

@yaakov-h
Copy link
Member

This appears to be a duplicate of #297.

@TheUnlocked
Copy link
Author

You seem to be right. I guess when I searched around for this I didn't search hard enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants