Skip to content

How to Resolve Error "CS0248: Cannot create an array with a negative size" in C#

Learn how to fix the C# compiler error CS0248 when attempting to create an array with a negative size, including common causes like typos, constant calculations, and runtime vs. compile-time checks.

The Compiler Error CS0248 is a logical definition error. The message reads: "Cannot create an array with a negative size".

In C#, standard arrays (allocated on the heap using the new keyword) must have a length of 0 or greater. It is physically impossible to reserve memory for a negative number of items. This error occurs specifically when the compiler detects that the size provided to the array constructor is a constant value (a literal or a const variable) that is negative.

This guide explains why this check exists and how to ensure your array definitions are valid.

Understanding Array Dimensions

When you declare new int[5], you are asking the .NET Runtime to allocate a block of memory sufficient to hold exactly 5 integers.

  • Positive Size: Valid.
  • Zero Size (new int[0]): Valid. This creates an empty array object (useful for returning "no results" without returning null).
  • Negative Size: Invalid. The concept of "negative count" does not exist in memory allocation.

CS0248 catches this impossible request at compile-time to prevent you from deploying code that is guaranteed to crash.

Scenario 1: Negative Literals (Typos)

The most obvious cause is accidentally typing a minus sign - inside the square brackets.

Example of error

public class DataStorage
{
    public void Initialize()
    {
        // ⛔️ Error CS0248: Cannot create an array with a negative size
        // You cannot have an array with negative 10 elements.
        int[] numbers = new int[-10];
    }
}

Solution: Use Positive Integers

Remove the negative sign.

public class DataStorage
{
    public void Initialize()
    {
        // ✅ Correct: Allocates space for 10 integers.
        int[] numbers = new int[10];
    }
}

Scenario 2: Constant Calculations

This error also triggers if you use a const variable or a mathematical expression involving constants that evaluates to a negative number. Because const values are calculated by the compiler, it "sees" the result immediately.

Example of error

public class GridConfig
{
    const int HeaderSize = 50;
    const int TotalSpace = 10;

    public void Setup()
    {
        // ⛔️ Error CS0248: The compiler evaluates (10 - 50) = -40.
        // It knows "Payload" will be -40, so it blocks the array creation.
        byte[] buffer = new byte[TotalSpace - HeaderSize];
    }
}

Solution: Correct the Constants

Ensure your constants result in a non-negative number.

public class GridConfig
{
    const int HeaderSize = 10;
    const int TotalSpace = 50;

    public void Setup()
    {
        // ✅ Correct: (50 - 10) = 40.
        byte[] buffer = new byte[TotalSpace - HeaderSize];
    }
}

Runtime vs. Compile-Time Checks

It is important to note that CS0248 only applies when the size is known at compile time.

If you use a variable (not const) that happens to be negative while the program is running, the code will compile successfully, but it will throw a System.OverflowException at runtime.

Compile-Time (CS0248)

const int size = -5;
int[] arr = new int[size]; // ❌ Won't Compile (CS0248)

Runtime (Exception)

int size = -5;
int[] arr = new int[size]; // ✅ Compiles, but CRASHES when run

Conclusion

CS0248 is a straightforward logic check.

  1. Check the Brackets: Look inside new Type[x].
  2. Check the Sign: Did you type -?
  3. Check the Math: If using const variables, trace the math to ensure the result is not negative.
Navigation