Number of items
Count the items in a list. Repeated values each count as one item.
values.Length
Simple example
Start4 4 7
Result3
using System;
int[] values = new int[] { 4, 4, 7 };
Console.WriteLine(values.Length);
3
Advertisements
Everyday examples
A delivery list has parcel numbers 41, 17 and 63. How many parcels are due?
Start41 17 63
Result3
using System;
int[] parcels = new int[] { 41, 17, 63 };
Console.WriteLine(parcels.Length);
3
The passenger list contains ticket numbers 8 and 11. How many passengers are listed?
Start8 11
Result2
using System;
int[] tickets = new int[] { 8, 11 };
Console.WriteLine(tickets.Length);
2
Four quiz scores are 2, 2, 5 and 3. How many attempts were recorded, including repeats?
Start2 2 5 3
Result4
using System;
int[] scores = new int[] { 2, 2, 5, 3 };
Console.WriteLine(scores.Length);
4
An empty list has zero items. Its length says how many items it holds.
Run the example
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson