What is best way to loop through an enumeration looking for a matching value?
string match = "A";
enum Sample { A, B, C, D }
foreach(...) {
//should return Sample.A
}
An enum can be looped through using Enum. GetNames<TEnum>() , Enum.
you can iterate the elements like: for(int i=Bar; i<=Last; i++) { ... } Note that this exposes the really-just-an-int nature of a C enum. In particular, you can see that a C enum doesn't really provide type safety, as you can use an int in place of an enum value and vice versa.
Yes. It iterates over an std::initializer_list<Item>. link.
You're looking for Enum.Parse
:
Sample e = (Sample)Enum.Parse(typeof(Sample), match);
You can loop through the values by calling Enum.GetValues
or Enum.GetNames
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With