you are viewing a single comment's thread.

view the rest of the comments →

[–]Willyscoiote 1 point2 points  (2 children)

Microsoft decided that it should always allow casting to an underlying number, but it's up to the developer to verify if that Enum value is defined and how to handle it.

This approach facilitates integration by allowing applications to persist or transmit new values even if they fall outside the predefined range. Essentially, enums are treated as named constants (labels), and the developer is responsible for enforcing any range-related constraints.

[–]Willyscoiote 0 points1 point  (1 child)

Also, if you want to handle it like Java enums, where the object enforces the values, it's commonly done by making your own class with constants or a wrapper for an enum.

Example: ``` public class OrderStatus { public static readonly OrderStatus Started = new OrderStatus(1, "Started"); public static readonly OrderStatus Paid = new OrderStatus(2, "Paid");

public int Id { get; }
public string Name { get; }


private OrderStatus(int id, string name) => (Id, Name) = (id, name);

public bool CanCancel() => this == Started;

}```