Why is the enumeration value from a multi dimensional array not equal to itself?

前端 未结 2 956
难免孤独
难免孤独 2021-01-30 19:28

Consider:

using System;

public class Test
{
    enum State : sbyte { OK = 0, BUG = -1 }

    static void Main(string[] args)
    {
        var s = new State[1,          


        
2条回答
  •  走了就别回头了
    2021-01-30 19:35

    Let's consider OP's declaration:

    enum State : sbyte { OK = 0, BUG = -1 }
    

    Since the bug only occurs when BUG is negative (from -128 to -1) and State is an enum of signed byte I started to suppose that there were a cast issue somewhere.

    If you run this:

    Console.WriteLine((sbyte)s[0, 0]);
    Console.WriteLine((sbyte)State.BUG);
    Console.WriteLine(s[0, 0]);
    unchecked
    {
        Console.WriteLine((byte) State.BUG);
    }
    

    it will output :

    255

    -1

    BUG

    255

    For a reason that I ignore(as of now) s[0, 0] is cast to a byte before evaluation and that's why it claims that a == s[0,0] is false.

提交回复
热议问题