Bird
Raised Fist0
DSA C#programming

Array Declaration and Initialization in DSA C#

Choose your learning style10 modes available
Mental Model
An array is a fixed-size collection of items stored one after another in memory. Declaring and initializing an array means creating this collection and filling it with values.
Analogy: Think of an egg carton with fixed slots. Declaring the array is like getting the empty carton with a set number of slots, and initializing is like placing eggs in each slot.
Index:  [0] -> [1] -> [2] -> [3] -> [4]
Value:  [ ]    [ ]    [ ]    [ ]    [ ]
Dry Run Walkthrough
Input: Declare an array of 5 integers and initialize with values 10, 20, 30, 40, 50
Goal: Create an array with 5 slots and fill each slot with the given values
Step 1: Declare an integer array with size 5
Index:  [0] -> [1] -> [2] -> [3] -> [4]
Value:  [0]    [0]    [0]    [0]    [0]
Why: We reserve space for 5 integers, initially all set to zero by default
Step 2: Assign 10 to array[0]
Index:  [0] -> [1] -> [2] -> [3] -> [4]
Value:  [10]   [0]    [0]    [0]    [0]
Why: We put the first value in the first slot
Step 3: Assign 20 to array[1]
Index:  [0] -> [1] -> [2] -> [3] -> [4]
Value:  [10]   [20]   [0]    [0]    [0]
Why: We put the second value in the second slot
Step 4: Assign 30 to array[2]
Index:  [0] -> [1] -> [2] -> [3] -> [4]
Value:  [10]   [20]   [30]   [0]    [0]
Why: We put the third value in the third slot
Step 5: Assign 40 to array[3]
Index:  [0] -> [1] -> [2] -> [3] -> [4]
Value:  [10]   [20]   [30]   [40]   [0]
Why: We put the fourth value in the fourth slot
Step 6: Assign 50 to array[4]
Index:  [0] -> [1] -> [2] -> [3] -> [4]
Value:  [10]   [20]   [30]   [40]   [50]
Why: We put the fifth value in the last slot
Result:
Index:  [0] -> [1] -> [2] -> [3] -> [4]
Value:  [10]   [20]   [30]   [40]   [50]
Annotated Code
DSA C#
using System;

class Program
{
    static void Main()
    {
        // Declare an integer array of size 5
        int[] numbers = new int[5];

        // Initialize each element
        numbers[0] = 10; // assign 10 to first slot
        numbers[1] = 20; // assign 20 to second slot
        numbers[2] = 30; // assign 30 to third slot
        numbers[3] = 40; // assign 40 to fourth slot
        numbers[4] = 50; // assign 50 to fifth slot

        // Print the array elements
        for (int i = 0; i < numbers.Length; i++)
        {
            Console.Write(numbers[i]);
            if (i < numbers.Length - 1)
                Console.Write(" -> ");
        }
        Console.WriteLine(" -> null");
    }
}
int[] numbers = new int[5];
Reserve space for 5 integers, initially zero
numbers[0] = 10; // assign 10 to first slot
Fill first slot with 10
numbers[1] = 20; // assign 20 to second slot
Fill second slot with 20
numbers[2] = 30; // assign 30 to third slot
Fill third slot with 30
numbers[3] = 40; // assign 40 to fourth slot
Fill fourth slot with 40
numbers[4] = 50; // assign 50 to fifth slot
Fill fifth slot with 50
for (int i = 0; i < numbers.Length; i++)
Loop through all array slots to print values
Console.Write(numbers[i]);
Print current slot value
OutputSuccess
10 -> 20 -> 30 -> 40 -> 50 -> null
Complexity Analysis
Time: O(n) because we assign values to each of the n array slots once
Space: O(n) because the array stores n elements in contiguous memory
vs Alternative: Compared to dynamic lists, arrays have fixed size but faster access and simpler memory layout
Edge Cases
Declare array with size zero
Creates an empty array with no slots, no values to assign
DSA C#
int[] numbers = new int[0];
Initialize array with fewer values than size
Unassigned slots remain at default zero value
DSA C#
numbers[0] = 10;
When to Use This Pattern
When you need a fixed-size collection of items with fast access by position, use array declaration and initialization to set up the storage.
Common Mistakes
Mistake: Trying to assign a value outside the declared array size
Fix: Ensure index is within 0 to array length minus one
Summary
Creates a fixed-size collection of elements stored in order.
Use when you know the number of elements in advance and need fast access.
Remember arrays reserve space first, then you fill each slot with values.

Practice

(1/5)
1. Which of the following is the correct way to declare and initialize an integer array with 3 elements in C#?
easy
A. int[] numbers = new int[3] {1, 2, 3};
B. int numbers = new int[3] {1, 2, 3};
C. int numbers[] = {1, 2, 3};
D. int[] numbers = new int(3) {1, 2, 3};

Solution

  1. Step 1: Understand array declaration syntax

    In C#, to declare an array, you use the type followed by square brackets, then assign with new keyword and specify size and elements.
  2. Step 2: Check each option for correctness

    int[] numbers = new int[3] {1, 2, 3}; correctly declares an integer array with size 3 and initializes it with values 1, 2, 3. int numbers = new int[3] {1, 2, 3}; misses square brackets for array type. int numbers[] = {1, 2, 3}; is invalid syntax - square brackets must follow the type name, not the variable name. int[] numbers = new int(3) {1, 2, 3}; uses parentheses instead of square brackets which is invalid.
  3. Final Answer:

    int[] numbers = new int[3] {1, 2, 3}; -> Option A
  4. Quick Check:

    Correct array declaration uses int[] and new int[size] [OK]
Hint: Use type[] name = new type[size] {values}; for arrays [OK]
Common Mistakes:
  • Forgetting square brackets [] after type
  • Using parentheses () instead of [] for size
  • Missing the new keyword
  • Assigning array without brackets
2. Which of the following is a syntax error when declaring an array in C#?
easy
A. int[] arr = new int[5];
B. int[] arr = {1, 2, 3};
C. int arr[] = new int[5];
D. int[] arr = new int[] {1, 2, 3};

Solution

  1. Step 1: Review C# array declaration syntax

    C# requires the square brackets to be placed after the type, not after the variable name. So int[] arr is correct, but int arr[] is invalid syntax.
  2. Step 2: Check each option

    int[] arr = new int[5]; is correct standard syntax. int arr[] = new int[5]; uses C/C++ style int arr[] which is invalid in C#. int[] arr = {1, 2, 3}; is valid shorthand initialization. int[] arr = new int[] {1, 2, 3}; is also valid with explicit new int[].
  3. Final Answer:

    int arr[] = new int[5]; -> Option C
  4. Quick Check:

    Square brackets go after type, not variable name [OK]
Hint: Remember: In C#, brackets go after type, not variable name [OK]
Common Mistakes:
  • Using C/C++ style array declaration
  • Placing brackets after variable name
  • Confusing array initialization syntax
3. You want to declare and initialize a string array with the names "Alice", "Bob", and "Charlie" in one line. Which of the following is the best way to do this in C#?
easy
A. string[] names = new string[3]; names = {"Alice", "Bob", "Charlie"};
B. string[] names = new string[] {"Alice", "Bob", "Charlie"};
C. string names[] = new string[3] {"Alice", "Bob", "Charlie"};
D. string names[] = {"Alice", "Bob", "Charlie"};

Solution

  1. Step 1: Understand array declaration and initialization rules

    In C#, you can declare and initialize an array in one line using new string[] { ... } or shorthand { ... } only if declaration and initialization are combined.
  2. Step 2: Evaluate each option

    string[] names = new string[3]; names = {"Alice", "Bob", "Charlie"}; tries to assign values after declaration without new, which is invalid. string names[] = {"Alice", "Bob", "Charlie"}; is invalid syntax with brackets after variable name. string names[] = new string[3] {"Alice", "Bob", "Charlie"}; uses invalid syntax with brackets after variable name. string[] names = new string[] {"Alice", "Bob", "Charlie"}; correctly declares and initializes in one line.
  3. Final Answer:

    string[] names = new string[] {"Alice", "Bob", "Charlie"}; -> Option B
  4. Quick Check:

    Use new string[] { ... } for clear array initialization [OK]
Hint: Use new string[] { ... } for clear one-line initialization [OK]
Common Mistakes:
  • Assigning array values without new keyword after declaration
  • Using C-style array syntax
  • Confusing declaration and assignment
4. What will be the output of the following C# code?
int[] arr = new int[4] {10, 20, 30, 40};
Console.WriteLine(arr[2]);
medium
A. 30
B. 20
C. 40
D. IndexOutOfRangeException

Solution

  1. Step 1: Understand array indexing

    Arrays in C# are zero-indexed, meaning the first element is at index 0. So arr[0] = 10, arr[1] = 20, arr[2] = 30, arr[3] = 40.
  2. Step 2: Identify the value at index 2

    The code prints arr[2], which is the third element, value 30.
  3. Final Answer:

    30 -> Option A
  4. Quick Check:

    Zero-based index means arr[2] = 30 [OK]
Hint: Remember arrays start at index 0 in C# [OK]
Common Mistakes:
  • Confusing index 2 with third element value
  • Expecting 1-based indexing
  • Mistaking array length for last index
5. Identify the error in the following C# code snippet:
int[] numbers = new int[3];
numbers = {1, 2, 3};
medium
A. No error, code runs fine
B. Array size mismatch error
C. Cannot assign values without new keyword
D. Syntax error due to missing new keyword in assignment

Solution

  1. Step 1: Analyze array initialization and assignment

    In C#, you can initialize an array with values only at declaration or by using new int[] { ... }. Direct assignment like numbers = {1, 2, 3}; is invalid syntax.
  2. Step 2: Identify the syntax error

    The second line misses the new int[] part, causing a syntax error.
  3. Final Answer:

    Syntax error due to missing new keyword in assignment -> Option D
  4. Quick Check:

    Array assignment requires new int[] { ... } [OK]
Hint: Use new int[] { ... } when assigning arrays after declaration [OK]
Common Mistakes:
  • Assigning array values without new keyword
  • Trying to reassign array with braces only
  • Confusing initialization with assignment
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard