Integer to enum casting in C#

This question already has an answer here:

  • What does the [Flags] Enum Attribute mean in C#? 10 answers

  • Let's say your enum is this:

    enum E {
        A = 1
        B = 2
        C = 4
        D = 8
    }
    

    If you translate each value to it's binary value, you get:

    A = 0001
    B = 0010
    C = 0100
    D = 1000
    

    Now let's say you try to convert 12 , 1100 in binary, to this enum :

    E enumTest = (E)12;
    

    It will output C|D

    | being the OR operator, C|D would mean 0100 OR 1000 , which gives us 1100 , or 12 in decimal.

    In your case, it's just trying to find which enum combination is equal to 67239937 . This is converted to binary as 100000000100000000000000001 . This then converts to Test2|Test16|Test20 or 1|(1 << 17)|(1 << 26) .

    链接地址: http://www.djcxy.com/p/54436.html

    上一篇: 如何在枚举中保留一个枚举列表

    下一篇: 整数在C#中枚举铸造