×

c# · level 1 · unit 6 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

An keeps many values together in one box.

An array is like an egg box: one box with a row of slots. Put the items between curly brackets, split by commas: string[] pets = { "cat", "dog" };. The square brackets mean many strings, not one.

Every must be the same type, so an int[] holds whole numbers only. pets.Length tells you how many items there are, with no brackets, as with a string.

Print an array on its own and C# only shows its type. To see every item, use . It glues the items into one string, with a comma and a space between each.

using System;

string[] pets = { "cat", "dog", "goldfish" };
int[] ages = { 3, 5, 1 };
Console.WriteLine(pets.Length);
Console.WriteLine(string.Join(", ", pets));
Console.WriteLine(string.Join(" and ", ages));

The first string you give string.Join is the gap it puts between the items.