ArgumentOutOfRangeException - throw helpers for enum argument #118169
-
Hi all, Just wanted to start discussion on this. Once we figure out what exactly we're wanting here, I can create a proper API suggestion. Starting with Net7, we have been adding static helper methods to commonly used exception types to handle common scenarios. It is a very common scenario to accept enum arguments, but the existing We could create a new exception type for this, but I think I think the most common scenario is likely the case where an enum value is not defined. For instance: enum Animals
{
None = 0,
Dog = 1,
Cat = 2
}
public void LogAnimal(Person person, Animals animal)
{
string animalType = animal switch
{
Animals.None => "no pets!",
Animals.Dog => "a dog!",
Animals.Cat => "a cat!",
_ => // Throw an exception!
};
Console.WriteLine($"{person.Name} has {animalType}");
} For non-flags enums, I think two extensions will be helpful:
For flags enums, we will need to figure out how to handle composite values. For non-composite values, we can use the same logic as above. But for composite values, it may be a good idea to have an argument to indicate whether composite values are automatically invalid (if Are there any other enum helpers you might find viable for this? Or is it worth looking into creating a new exception type entirely, like |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Related to #118053 |
Beta Was this translation helpful? Give feedback.
-
This was discussed and rejected number of times, see e.g. #83107, #92071 |
Beta Was this translation helpful? Give feedback.
This was discussed and rejected number of times, see e.g. #83107, #92071