C#C
C#7mo ago
Jason_Bjorn

Best way to set all applicable flags?

I have a string that represent possible flags. How can I set all the flags that are in the string?
Classifiers getClassifiers(string s) {
    Classifiers c = new();
    foreach (var enumVal in Enum.GetValues(typeof(Classifiers)))
    {
        if (s.Contains(enumVal.ToString())) c += (int)enumVal;
    }
    
    return c;
}

Classifiers c = getClassifiers("CTR HI LAG");
Console.WriteLine(c); // HI, CTR, LAG

[Flags]
enum Classifiers
{
    LO = 1<<0,
    HI = 1<<1,
    LFT = 1<<2,
    RIT = 1<<3,
    CTR = 1<<4,
    LAG = 1<<5
}


This works but is there a better way?
Was this page helpful?