[Proposal] Smart Enums In C# Like Java #5937
Replies: 13 comments 43 replies
-
While I think that works and doesn't look half bad, what is the reason you cannot use a combination of a repository and an enum or any arbitrary value as key, that returns some record? That could look like: |
Beta Was this translation helpful? Give feedback.
-
Oracles holds a patent on the implementation of enums in Java. IMO C# would do better to adopt Swift's version of enums instead which can also serve as discriminated unions. |
Beta Was this translation helpful? Give feedback.
-
Why not using a record? E.g.: using System;
var free = Subscription.FREE;
Console.WriteLine(free == Subscription.FREE);
public record struct Subscription {
public static readonly Subscription FREE = new("Free Tier", 0);
public static readonly Subscription PRIME = new("Premium Tier", 25);
public static readonly Subscription VIP = new("VIP Tier", 50);
public String Name { get; }
public int Discount { get; }
public Subscription(String name, int discount) {
Name = name;
Discount = discount;
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
You Could Do Something Like This But Now For One Feature, You Have Two Different Data Structures And You Need To Merge Which Is Kinda Bad For Clean And Clear Code. |
Beta Was this translation helpful? Give feedback.
-
You are right, I was kinda planning ahead here. Ignore the repository, that is not relevant to your case. @bernd5 has a great and clean solution. If I was you I'd stick with that. In my opinion records are super clean and nice to work with. |
Beta Was this translation helpful? Give feedback.
-
You can decorate the enum options with attributes, and write extension methods to get the values set in those attributes, as needed. enum Color { [Name("Red")] Red }
public static class _Color_Extensions
{
public static string? GetName(this Color This) =>
typeof(Color).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)
.First(f=>f.Name == This.ToString())
.GetCustomAttributes<NameAttribute>().FirstOrDefault()?.Name;
static readonly string[] _names = new[] { "Red" }; // or use a cache
static readonly string[] _names = Enum.GetValues(typeof(Color)).Select(...).ToArray(); // you can also populate from attributes, during static init
public static string? GetName(this Color This) => _names[(int)This];
} (The above code is un-tested - just wanted to give you an idea.) |
Beta Was this translation helpful? Give feedback.
-
While I also would like to see "smart" enums in C#, I'd like to ask: was it intentional that you opened the exact same issue/discussion two times? |
Beta Was this translation helpful? Give feedback.
-
This is a much needed improvement to C#. Writing an extension class or record class/struct just to get properties and methods is a bad smell from a language perspective especially in 2023. We have modern languages that support a variety of language level features. Can we get an update on what's happened to this proposal? Whether it's considered to be implemented in future versions or not? Also, none of the reasons mentioned by commenters that are against this proposal work/justify why this is not needed in C#. C# as language still needs a lot of improvements and it doesn't need to stick onto older ways of coding. Modern languages like Swift, Rust, Carbon, Kotlin are evolving and they have more developer friendly features. Hoping C# stands out as the best choice even in next 10 years. |
Beta Was this translation helpful? Give feedback.
-
What Java has was never an enumerated type, it was just called an |
Beta Was this translation helpful? Give feedback.
-
the US patent expires next month! currently I'm resorting to Joshua Bloch's "Typesafe Enum" design-pattern detailed in Item 21 of Effective Java, first edition. in the meanwhile I'll explore C#'s Source Generators to relieve the insane boilerplate I currently have to maintain. if anything similar is added to C#, it will finally achieve a fraction of expressiveness of X Macros from C/C++!!! |
Beta Was this translation helpful? Give feedback.
-
With |
Beta Was this translation helpful? Give feedback.
-
This proposal is about
enums
which is a pretty useful type in object-oriented programming but the thing that is missing from enum in C# is a feature from java that enums can be like classes a bit like they can have special properties, and have constructors and more...An example for it will be a simple enum
Subscription
In Java:
In C#:
In C# I am not able to add constructors, properties, and methods. although there is a library to do this
SmartEnum
but wouldn't be nice if we have this feature built-in to C# where we could do something like thisBeta Was this translation helpful? Give feedback.
All reactions