Better way to get all enum constants from combined enum value

c#, enums

Solution

List<Type> types = Enum
                     .GetValues(typeof(Type))
                     .Cast<Type>()
                     .Where(val => (val & type) == val)
                     .ToList();

Another way getting desired result.

Problem

I'm trying to get all the enum values from the Type enum variable : ``` [Flags] enum Type { XML = 1, HTML = 2, JSON = 4, CVS = 8 } static void Main(string[] args) { Type type = Type.JSON | Type.XML; List<Type> types = new List<Type>(); foreach (string elem in type.ToString().Split(',') ) types.Add( (Type)Enum.Parse( typeof(Type), elem.Trim() ) ); } ``` Is there a better way to do that ?

Original source

Related problems