2

I have following declaration in my class.

public class MyClass
{
    private const long SOME_VALUE= (10 * 1024 * 1024 * 1024);  // 10 GB
    ....
}

However compiler is reporting following error

error CS0220: The operation overflows at compile time in checked mode

According to MSDN.

enter image description here

As far as I can tell, SOME_VALUE is in this range for long type. Any thoughts on why am I getting this compile time error?

2 Answers 2

4

Each of the individual values in the calculation are int so the compiler multiplies them together as ints, hence the overflow. The easiest solution is to mark one or all of them as a long using the L suffix, this will force the calculation to be done as a long:

private const long SOME_VALUE= 10L * 1024 * 1024 * 1024;
Sign up to request clarification or add additional context in comments.

Comments

2

Add L suffix:

public class MyClass
{
    private const long SOME_VALUE= (10L * 1024L * 1024L * 1024L);  // 10 GB
    ....
}

Without L suffix (stands for long) compiler treats the expression as of int one and warns about integer overflow.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.