Search code examples
c#overflowvariable-assignment

Overflow in constant value computation error in C#


I need to assign constant value in integer (or other data type).

I got "Cannot convert ..." error with the assignment.

enter image description here

Casting doesn't seem to work with "Overflow ..." error.

enter image description here

What's wrong with this?


Solution

  • You've defined too many Fs in the constant. Using 0xFFFFFFFF the compiler must choose a a storage location that supports a positive value of 0xFFFFFFFF. The max positive value of an Int32 is instead 0x7FFFFFFF and hence the compiler correctly errors. The only types which can hold 0xFFFFFFFF are uint or one of the 64 bit storages.

    To fix this just use Int32.MaxValue

    int i32 = Int32.MaxValue;