[C#]

Shift Your C# Enums

If you want easily increment your flags in Enum, you can represent it's values with bitwise shift operator:

[Flags]
enum ProgrammingLanguages
{
    None        = 0,
    C           = 1 << 0,
    Cpp         = 1 << 1,
    VisualBasic = 1 << 2,
    VisualCpp   = 1 << 3,
    Rust        = 1 << 4,
    CSharp      = 1 << 5,
    Java        = 1 << 6,
    Php         = 1 << 7,
    Go          = 1 << 8,
    Rust        = 1 << 9,
    Scala       = 1 << 10,
    Clojure     = 1 << 11,
    FSharp      = 1 << 12,
    DotNetFamily = VisualBasic | VisualCpp | CSharp,
    JvmFamily = Java | Scala | Clojure
}

Performance is equal, cause in code all enums translated into underlying numbers by compiler itself, so no runtime overhead. Seems a little bit more trivial, than multiplying "by hand":

[Flags]
enum ProgrammingLanguages
{
    None        = 0,
    C           = 1,
    Cpp         = 2,
    VisualBasic = 4,
    VisualCpp   = 8,
    Rust        = 16,
    CSharp      = 32,
    Java        = 64,
    Php         = 128,
    Go          = 256,
    Rust        = 512,
    Scala       = 1024,
    Clojure     = 2048,
    FSharp      = 4096,
    DotNetFamily = VisualBasic | VisualCpp | CSharp,
    JvmFamily = Java | Scala | Clojure
}

More info about bitwise operators and Enums you can learn from this post.

Hi!

My name is Alexey and I'm a software engineer.

I'm specialized mostly in developing distributed and highload systems. Working with C#, Golang and Kotlin. My recent work experience is on LinkedIn and my projects are available on GitHub.

© 2024 Alexey Maltsev