Methods inside enum in C #

In Java, you can have methods inside an enumeration.

Is there such a possibility in C # or is it just a collection of strings and that's it?

I tried to override ToString() but it does not compile. Does anyone have a simple code example?

+133
enums c # enumeration
May 12 '11 at 23:24
source share
6 answers

You can write extension methods for enumerated types:

 enum Stuff { Thing1, Thing2 } static class StuffMethods { public static String GetString(this Stuff s1) { switch (s1) { case Stuff.Thing1: return "Yeah!"; case Stuff.Thing2: return "Okay!"; default: return "What?!"; } } } class Program { static void Main(string[] args) { Stuff thing = Stuff.Thing1; String str = thing.GetString(); } } 
+230
May 12 '11 at 11:30
source share

You can write an extension method for your listing:

How to create a new method for listing (C # Programming Guide)

+19
May 12 '11 at 23:29
source share

Another option is to use the enumeration class created by Jimmy Bogard .

In essence, you must create a class that inherits its Enumeration . Example:

 public class EmployeeType : Enumeration { public static readonly EmployeeType Manager = new EmployeeType(0, "Manager"); public static readonly EmployeeType Servant = new EmployeeType(1, "Servant"); public static readonly EmployeeType Assistant = new EmployeeType(2, "Assistant to the Regional Manager"); private EmployeeType() { } private EmployeeType(int value, string displayName) : base(value, displayName) { } // Your method... public override string ToString() { return $"{value} - {displayName}!"; } } 

Then you can use it as an enum with the ability to put methods into it (among other things):

 EmployeeType.Manager.ToString(); //0 - Manager EmployeeType.Servant.ToString(); //1 - Servant EmployeeType.Assistant.ToString(); //2 - Assistant to the Regional Manager 

You can download it from NuGet .

Although this implementation is not native to the language, the syntax (construction and use) is pretty close to languages ​​that implement enumerations initially better than C # (e.g. Kotlin ).

+10
Aug 16 '16 at 22:51
source share

Nope. You can create a class and then add a bunch of properties to the class to emulate the enum somewhat, but this is actually not the same.

 class MyClass { public string MyString1 { get{ return "one";} } public string MyString2 { get{ return "two";} } public string MyString3 { get{ return "three";} } public void MyMethod() { // do something. } } 

It would be best to put your methods in a class separate from your emum.

+3
May 12 '11 at 23:28
source share

Since I came across and needed the exact opposite of enum to string, here is a general solution:

 static class EnumExtensions { public static T GetEnum<T>(this string itemName) { return (T) Enum.Parse(typeof(T), itemName, true); } } 

It also ignores case and is very convenient for parsing REST-Response for your enumeration for more type safety. Hope this helps someone

0
Jun 11 '19 at 21:20
source share

C # Does not allow the use of methods in counters, since this is not a class-based principle, but rather a two-dimensional array with a string and value.

Using classes in this case is strongly discouraged by Microsoft; instead, use (data) struct (); STRUCT is designed as a lightweight class for data and its handlers, and can perform functions perfectly. C # and its compiler do not have tracking and performance capabilities, as is known from JAVA, where the more specific class / method is used, the faster it starts and its use becomes "expected". C # just doesn’t have this, so use STRUCT instead of CLASS to differentiate.

-9
Jan 26 '15 at 7:12
source share



All Articles