Support for a default enum value?
- Dominant language
- C#
- Stars
- 2.4k
- Forks
- 184
- PR merge metrics
- No merged PRs in 30d
Description
@ardalis, I am wondering what your thoughts are regarding the idea of having the option of specifying a default enum value for a class inheriting from SmartEnum.
For example, let's say I have the following enum:
```cs
public class Status : SmartEnum
{
public static readonly Status Other = new(nameof(Pending), 0);
public static readonly Status Pending = new(nameof(Pending), 1);
public static readonly Status Completed = new(nameof(Completed), 2);
private Status(string name, int value) : base(name, value) { }
}
```
Then, if I were to do this:
```cs
var status = Status.FromName("Cancelled");
```
An exception gets thrown. Instead, I would prefer to have the `Other` status returned if an unknown is specified in the `FromName` method. How would you recommend solving this? Would there be any benefit to adding a new constructor to `SmartEnum` that allows an enum to specify whether it is to be used as a default value?
Contributor guide
Research direction
Start by reviewing the SmartEnum FromName API and its current exception behavior. Define how a derived enum designates a fallback such as Other, then specify and test the behavior for unknown names while preserving existing known-name lookups.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100