Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are all the possible C# array initialization syntaxes?
C# provides multiple ways to initialize arrays, giving you flexibility in how you declare and populate arrays with values. Each syntax has its specific use cases depending on whether you know the array size in advance or want to provide initial values immediately.
Array Initialization Syntax Options
Explicit Size with Values
You can specify the array size explicitly along with initial values −
int[] marks = new int[5] { 99, 98, 92, 97, 95 };
Implicit Size with Values
The array size can be inferred from the number of elements provided −
int[] marks = new int[] { 99, 98, 92, 97, 95 };
Array Literal Syntax
The simplest form, where both type and size are inferred −
int[] marks = { 99, 98, 92, 97, 95 };
Declaration and Initialization Separately
You can declare an array first and initialize it later −
int[] marks;
marks = new int[] { 99, 98, 92, 97, 95 };
Empty Array with Size Only
Create an array with default values (0 for integers) −
int[] numbers = new int[10];
Example
Here's a complete example demonstrating different initialization approaches −
using System;
class ArrayInitialization {
static void Main(string[] args) {
// Method 1: Explicit size with values
int[] scores1 = new int[3] { 85, 90, 78 };
// Method 2: Implicit size with values
int[] scores2 = new int[] { 92, 88, 95 };
// Method 3: Array literal syntax
int[] scores3 = { 77, 83, 91 };
// Method 4: Empty array with size
int[] scores4 = new int[3];
scores4[0] = 89;
scores4[1] = 94;
scores4[2] = 86;
// Display all arrays
Console.WriteLine("Scores1: " + string.Join(", ", scores1));
Console.WriteLine("Scores2: " + string.Join(", ", scores2));
Console.WriteLine("Scores3: " + string.Join(", ", scores3));
Console.WriteLine("Scores4: " + string.Join(", ", scores4));
}
}
The output of the above code is −
Scores1: 85, 90, 78 Scores2: 92, 88, 95 Scores3: 77, 83, 91 Scores4: 89, 94, 86
Multidimensional Array Initialization
Multidimensional arrays can also be initialized using similar syntax patterns −
using System;
class MultiDimensionalArrays {
static void Main(string[] args) {
// 2D array initialization methods
int[,] matrix1 = new int[2,3] { {1, 2, 3}, {4, 5, 6} };
int[,] matrix2 = new int[,] { {7, 8, 9}, {10, 11, 12} };
int[,] matrix3 = { {13, 14, 15}, {16, 17, 18} };
// Jagged array initialization
int[][] jaggedArray = new int[2][];
jaggedArray[0] = new int[] { 1, 2, 3, 4 };
jaggedArray[1] = new int[] { 5, 6 };
Console.WriteLine("Matrix1[1,2]: " + matrix1[1,2]);
Console.WriteLine("Matrix2[0,1]: " + matrix2[0,1]);
Console.WriteLine("Matrix3[1,0]: " + matrix3[1,0]);
Console.WriteLine("JaggedArray[0][3]: " + jaggedArray[0][3]);
}
}
The output of the above code is −
Matrix1[1,2]: 6 Matrix2[0,1]: 8 Matrix3[1,0]: 16 JaggedArray[0][3]: 4
Comparison of Initialization Methods
| Method | Syntax | Use Case |
|---|---|---|
| Explicit Size | new int[5] { 1, 2, 3, 4, 5 } |
When you want to be explicit about array size |
| Implicit Size | new int[] { 1, 2, 3, 4, 5 } |
When size is determined by initial values |
| Array Literal | { 1, 2, 3, 4, 5 } |
Shortest syntax, only at declaration |
| Size Only | new int[5] |
When values will be assigned later |
Conclusion
C# offers flexible array initialization syntax options ranging from explicit size declaration to concise array literals. Choose the method that best fits your specific needs: use array literals for simple cases, explicit sizing when clarity is important, and separate declaration when values are assigned conditionally.
