c# list contains code example

Example 1: c# object list contaion parameters

public class A { public int Id { get; set; } public A(int id) { Id = id; } }

List<A> list = new List<A>() { new A() { Id = 1}, new A() { Id = 2} };

var matches = list.Where(item => item.Id == 1 || item.Id == 2);
// matches = [0, 1]
// can be used like
for (int i = 0; i < matches.Count; i++) { Console.WriteLine(list[i]); }

Example 2: list contains type c#

if (within.OfType<Ball>().Any())

Example 3: c# check list of objects for value

var matches = myList.Where(p => p.Name == nameToExtract);