[Proposal] Smart Enums In C# Like Java #5935
-
This proposal is about An example for it will be a simple enum In Java: public enum Subscription {
FREE("Free Tier"),
PRIME("Premium Tier"),
VIP("VIP Tier");
private final String name;
public Subscription(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
} In C#: public enum Subscription {
Free,
Prime,
Vip;
} In C# I am not able to add constructors, properties, and methods. although there is a library to do this public enum Subscription {
Free("Free Tier"),
Prime("Premium Tier"),
Vip("VIP Tier");
private const string Name;
public Subscription(string bame) {
Name = name;
}
public string GetName()
{
return Name
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
It has been discussed a lot of times. A recent discussion is #5889. |
Beta Was this translation helpful? Give feedback.
-
The syntax isn't exactly the same, but what you're asking for can quite easily be written today with: public class Subscription
{
public static readonly Subscription Free = new("Free Tier");
public static readonly Subscription Prime = new("Premium Tier");
public static readonly Subscription Vip = new("VIP Tier");
public string Name { get; }
private Subscription(string name)
{
Name = name;
}
} Although you use it like an enum it's an ordinary class at this point so you're free to add whatever methods etc you like. |
Beta Was this translation helpful? Give feedback.
-
As pointed out by @Unknown6656 in the other discussion, this is a duplicate of #5937. |
Beta Was this translation helpful? Give feedback.
As pointed out by @Unknown6656 in the other discussion, this is a duplicate of #5937.