I have a flagged enum and need to retrieve the names of all values set on it.
I am currently taking advantage of the enum\'s ToString() method which returns the eleme
List GetRoleNames(Roles roles) =>
Enum.GetValues(typeof(Roles))
.Cast()
.Where(role => roles.HasFlag(role))
.Select(role => role.ToString())
.ToList();
void TestRoleSelection()
{
var selectedRoles = (Roles)6;
var roleNames = GetRoleNames(selectedRoles);
Console.WriteLine(string.Join(",", roleNames));
// Output: Admin,User
}
[Flags]
enum Roles
{
SuperAdmin = 1,
Admin = 2,
User = 4,
Anonymous = 8
}