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 3Notice that here
Lengthis a property and not a method, like instring.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyYou are given a code, your tasks are:
- Declare and initialize an array (of type
double) namedcoolNumswith the values: 3.14, 1.68, 9.99 (in this order!) - 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]);
}
}