I have an enum
public enum FileExtentions {
mp3,
mpeg
}
And I have a FileInfo of which I want to check if the extension is in the previous enum. I was hoping I could do a
FileExtensions.Any(e=>e.ToString().Equals(file.Extension));
But that would have been too awesome. Any ideas?
For comparing String to Enum type you should convert enum to string and then compare them. For that you can use toString() method or name() method. toString()- Returns the name of this enum constant, as contained in the declaration.
equals method uses == operator internally to check if two enum are equal. This means, You can compare Enum using both == and equals method.
What's the reason behind Any
… Equals
? Did you overlook Contains
?
bool result = Enum.GetNames(typeof(FileExtensions)).Contains("mp3");
While pressing submit I thought of the answer myself:
Enum.GetNames(typeof(FileExtensions)).Any(f=>f.Equals("."+file.Extension))
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