Menu
Coddy logo textTech

Array

Lesson 8 of 14 in Coddy's Strings and Arrays in C# course.

Arrays are used to store multiple values in a single variable.

To declare an array, define the variable type with square brackets:

int[] numbers;

Just like a string, an array is also a special type.

To initialize the array values, place the values in a comma-separated list, inside curly braces:

int[] numbers = {1, 5, 12, 435};

The same goes for any type, for example, a string one:

string[] names = {"Bob", "Alice", "John"};

To get the length of an array, use the Length property:

Console.WriteLine(names.Length); // Outputs 3

Notice that here Length is a property and not a method, like in string.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

You are given a code, your tasks are:

  1. Declare and initialize an array (of type double) named coolNums with the values: 3.14, 1.68, 9.99 (in this order!)
  2. Print the length of the array you declared.

Try it yourself

using System;

class Program {
    public static void Main(String[] args) {
        // Write code here
        
        // Don't change below this line
        Console.Write(coolNums[0]);
        Console.Write(", ");
        Console.Write(coolNums[1]);
        Console.Write(", ");
        Console.Write(coolNums[2]);
    }
}

All lessons in Strings and Arrays in C#