C# Create an Array with Specific Length

To create an Array with specific length in C#, declare a variable of array type and assign a new array object with the length of array mentioned in square brackets.

A quick example to create an array arr of type int with length 5 is

</>
Copy
int[] arr = new int[5];

Since, no initial values are mentioned for the elements of this array, elements would be initialized with default values.

Example

In the following example, we create an int array arr of length 5, and print the contents of this array to console.

Program.cs

</>
Copy
using System;

namespace CSharpExamples {
    class Program {
        static void Main(string[] args) {
            int[] arr = new int[5];

            Console.WriteLine("Array Contents :");
            for (int i=0; i < arr.Length; i++) {
                Console.WriteLine(arr[i]);
            }
        }
    }
}

Output

Array Contents :
0
0
0
0
0

Conclusion

In this C# Tutorial, we learned how to create an Array of specific length, with examples.