Find an item in a list
Check whether a particular value appears anywhere in a list. A single match is enough.
Enumerable.Contains
Simple example
Does the list contain two?
Start1 · 2 · 3
ResultTrue
using System;
using System.Linq;
var values = new int[] { 1, 2, 3 };
Console.WriteLine(values.Contains(2));
True
Advertisements
Everyday examples
Is ticket 17 on the pickup list?
Start15 · 17 · 20
ResultTrue
using System;
using System.Linq;
var values = new int[] { 15, 17, 20 };
Console.WriteLine(values.Contains(17));
True
Does this bus stop at stop 6?
Start2 · 4 · 8
ResultFalse
using System;
using System.Linq;
var values = new int[] { 2, 4, 8 };
Console.WriteLine(values.Contains(6));
False
Is parcel 42 on the van?
Start42 · 18 · 15
ResultTrue
using System;
using System.Linq;
var values = new int[] { 42, 18, 15 };
Console.WriteLine(values.Contains(42));
True
Run the example
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson