Creating an Array

In this lesson, we will look at how to create arrays in C#, what methods are available, and what is important to consider when initializing them.

Creating an array with a specified size

When creating an array, you must specify the number of elements it contains. This number cannot be changed. Initially, the elements will be filled with default values.
Fig. 1: Create an array with default values
Preview
Fig. 1: Create an array with default values
Line 1, Char 1
This array contains 3 numbers, initially all equal to 0.

Creating an array with initial values

You can fill an array with values immediately upon creation. The array size is set automatically.
Fig. 2: Creating an array with initial values
Preview
Fig. 2: Creating an array with initial values
Line 1, Char 1

Summary

In this lesson you learned:
  • how to create an array with a fixed size;
  • how to create an array with initial values;
  • that an array has a fixed length;

Exercises

  1. Create an array of 5 integers without setting element values. Print the array length.
Line 1, Char 1
  1. Create an array of 4 strings with names: "Anna", "Boris", "Victor", "Galina". Print the array length.
Line 1, Char 1
  1. Try to create an array with 0 elements. Print the array length.
Line 1, Char 1