Read an item by its position
Pick an item using its position in the list. Here we read the second item every time.
Array.GetValue
Simple example
Read the second number.
Start4 · 7 · 9
Result7
using System;
using System.Linq;
var values = new int[] { 4, 7, 9 };
Console.WriteLine(values.GetValue(1));
7
Advertisements
Everyday examples
Which ticket is second in the queue?
Start42 · 17 · 23
Result17
using System;
using System.Linq;
var values = new int[] { 42, 17, 23 };
Console.WriteLine(values.GetValue(1));
17
Which stop does the bus visit second?
Start8 · 6 · 2
Result6
using System;
using System.Linq;
var values = new int[] { 8, 6, 2 };
Console.WriteLine(values.GetValue(1));
6
What was the second thermometer reading?
Start19 · 21 · 20
Result21
using System;
using System.Linq;
var values = new int[] { 19, 21, 20 };
Console.WriteLine(values.GetValue(1));
21
Run the example
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson