How to Resolve Error "CS0270: Array size cannot be specified in a variable declaration" in C#
The Compiler Error CS0270 is a syntax error regarding Array Declaration. The message reads: "Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)".
In C#, arrays are Reference Types (stored on the heap), unlike in C or C++ where arrays can be declared on the stack with a fixed size in the variable definition. In C#, the variable declaration only defines the type of the array (e.g., int[]), not its size. The size is determined only when you actually allocate memory using the new keyword.
This guide explains the difference between declaration and allocation and how to fix this syntax error.
Understanding Declaration vs. Allocation
In C#, creating an array involves two distinct steps:
- Declaration: You tell the compiler, "I have a variable named
arrthat will hold a reference to an integer array."- Syntax:
int[] arr;
- Syntax:
- Allocation (Instantiation): You tell the runtime, "Please allocate memory for 10 integers."
- Syntax:
arr = new int[10];
- Syntax:
CS0270 happens when you try to combine these steps incorrectly by putting the size [10] on the left side (the declaration side). The type is int[], not int[10].
Scenario: The C/C++ Style Syntax
This is the most common cause, especially for developers transitioning from C++ or Java (where syntax varies slightly). You attempt to define the size inside the variable type definition.
Example of error:
public class DataStore
{
// ⛔️ Error CS0270: Array size cannot be specified in a variable declaration.
// The compiler sees '[10]' as part of the TYPE, which is invalid in C#.
int[10] numbers;
public void Process()
{
// ⛔️ Error CS0270: Same issue with local variables.
string[5] names;
}
}
Solution 1: Use the new Keyword
You must move the size specification to the right side of the assignment operator using the new keyword. This explicitly allocates the memory on the heap.
Solution:
public class DataStore
{
// ✅ Correct: Variable type is 'int[]'. Initialization uses 'new int[10]'.
int[] numbers = new int[10];
public void Process()
{
// ✅ Correct
string[] names = new string[5];
}
}
Solution 2: Use Collection Initializers
If you already know the values you want to put in the array, you don't even need to specify the size explicitly. C# can infer the size based on the number of elements you provide in curly braces {}.
Solution:
public class Config
{
// ✅ Correct: The compiler sees 3 elements and creates a 'new int[3]'.
int[] validIds = { 101, 102, 103 };
// You can also combine 'new' with initializers
string[] days = new string[] { "Mon", "Tue", "Wed" };
}
Advanced: Fixed Size Buffers (Unsafe Code)
There is one specific scenario where syntax resembling int[10] is valid: creating a Fixed Size Buffer inside a struct for Interop with unmanaged code. This requires the unsafe context and the fixed keyword.
The Special Case:
// Requires <AllowUnsafeBlocks>true</AllowUnsafeBlocks> in .csproj
public unsafe struct InteropData
{
// ✅ Correct: This creates a fixed buffer of 10 integers inline within the struct.
// This is NOT a standard C# array (System.Array).
public fixed int Buffer[10];
}
Use this only when necessary for high-performance memory manipulation or communicating with native C++ DLLs. For standard C# applications, always use Solution 1.
Conclusion
CS0270 is a syntax enforcement to keep C# type definitions clean.
- Left Side: Always declare the type as
Type[](empty brackets). - Right Side: Specify the size as
new Type[Size](brackets with numbers). - Alternatives: Use
{ value1, value2 }to infer the size automatically.