Cannot implicitly convert type 'uint' to 'int'

I try to declar this statement:

private static int[] MyArr = {0x243F6A88, 0x85A308D3, 0x13198A2E};

And got this error

Cannot implicitly convert type 'uint' to 'int'

What wrong am I

Thank



Answer this question

Cannot implicitly convert type 'uint' to 'int'

  • soanfu

    If you're coming from a C++ background, C# is much more strict with types of literals. 0x85A308D3 is not considered an int because the top bit is high, forcing it to upcast to a uint so it will fit, despite legally representing a valid int value. Use the unchecked keyword to avoid the error. For example:

    private static int[] MyArr = {0x243F6A88, unchecked((int)0x85A308D3), 0x13198A2E};


  • niroshanonline

    Simply, the value you're attempting to assing to an int value exceed it's "range" capacity.

    Integral values expressed using hexadecimal notation are interpreted as UNSIGNED values.

    You can use an unchecked explicitly cast or choose a wider type, depending on your needs.


  • Ri-Karou

    OK its work

    Thank you very much


  • Sergey Astapov

    Hexadecimal constant is interpreted as unsigned integer, you need to add casting:

    private static int[] MyArr = {(int)0x243F6A88, (int)0x85A308D3, (int)0x13198A2E};


  • tcarff

    Yes I'm agree... maybe my post was a bit confusing, I've already expressed the "range check rule" in the first period of my former post.
  • johnTheDeveloper

    Mr. BogoMips wrote:

    Integral values expressed using hexadecimal notation are interpreted as UNSIGNED values.

    Not necessarily. The type depends on the value you specify. The default type rules for numeric literals are the same whether you use decimal or hexadecimal representation.



  • Cannot implicitly convert type 'uint' to 'int'