In C#, List.Exists(match) asks: does at least one item pass my test? The test is a predicate, a tiny function that gets one item and answers true or false. You usually write it as a lambda like n => n > 10.
Read the lambda as "for each n, is n greater than 10?". Exists tries the items from the start and stops at the first true. It differs from Contains(), which can only test equality with one value.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.Exists(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.
Syntax
Pass a lambda that returns a bool for one item. You get one bool back for the whole list.
C#
List<int> numbers = new List<int> { 3, 8, 12 };
bool big = numbers.Exists(n => n > 10); // true
bool huge = numbers.Exists(n => n > 100); // false
Call
What it does
numbers.Exists(n => n > 10)
true if any number is above 10.
names.Exists(s => s.StartsWith("B"))
true if any name starts with "B".
numbers.Exists(IsNegative)
Pass a method name instead of a lambda when the test is reused.
Exists is O(n) and stops early on the first match. An empty list always gives false. A null predicate throws ArgumentNullException. The LINQ method Any(predicate) does the same thing on any collection, but Exists is built into List<T> (and Array.Exists is the array version).
Library declarations
C#
public bool Exists(Predicate<T> match);
Parameters
Parameter
Description
match
Required. A Predicate<T>: a lambda or method that takes one item and returns true or false. It must not be null.
Return Value
Return Value
Description
bool
true if at least one item passes the test. false if none does, or if the list is empty.
C# Examples
Example 1 — Is Any Number Big?
Test the same list with two different rules.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 3, 8, 12 };
Console.WriteLine(numbers.Exists(n => n > 10));
Console.WriteLine(numbers.Exists(n => n > 100));
Console.WriteLine(numbers.Exists(n => n % 2 == 1));
}
}
Output
True
False
True
How It Works
Lambda form.n => n > 10 is a one-line function. n stands for each item in turn.
First rule.3 and 8 fail, then 12 passes. The scan stops there and the answer is True.
Second rule. No number is above 100, so every item fails and the answer is False.
Any rule works.n % 2 == 1 asks for an odd number. 3 is odd, so this one is True at the very first item.
Example 2 — Names and an Empty List
Check strings with a text rule, and see what an empty list answers.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> names = new List<string> { "Asha", "Ben", "Chen" };
List<string> none = new List<string>();
Console.WriteLine(names.Exists(s => s.StartsWith("B")));
Console.WriteLine(names.Exists(s => s.Length > 6));
Console.WriteLine(none.Exists(s => true));
}
}
Output
True
False
False
How It Works
Text rule.s.StartsWith("B") is true for "Ben", the second item, so the first line prints True.
Length rule. The longest name has four letters, so s.Length > 6 never passes and the second line is False.
Nothing to test. An empty list has no item that could pass, so the answer is False.
Even for always-true tests. The lambda s => true is never called here, because there are zero items. Exists still answers False.
Example 3 — Check a Property of Each Object
Use Exists to avoid adding an object whose name is already in the list.
C#
using System;
using System.Collections.Generic;
class Person {
public string Name;
public int Age;
}
class Program {
static void Main() {
List<Person> people = new List<Person> {
new Person { Name = "Asha", Age = 30 },
new Person { Name = "Ben", Age = 17 }
};
if (!people.Exists(p => p.Name == "Asha")) {
people.Add(new Person { Name = "Asha", Age = 99 });
}
Console.WriteLine(people.Count);
Console.WriteLine(people.Exists(p => p.Age < 18));
}
}
Output
2
True
How It Works
Rule on a field.p.Name == "Asha" looks inside each Person object, which Contains cannot do without an Equals override.
Already there. The first person is "Asha", so Exists returns True. The ! flips it, and the Add is skipped.
Count stays 2. Because the guard blocked the duplicate, the list still has the two original people.
Another rule.p.Age < 18 is true for "Ben" who is 17, so the last line is True.
Frequently Asked Questions
It returns true if at least one item in the list passes the test you give it, and false otherwise.
Contains checks one value by equality. Exists takes a predicate lambda, so it can test any rule, for example "is any number above 10?".
false, because there is no item that could pass the test.
They give the same answer. Exists is a method of List<T> and Array. Any(predicate) comes from System.Linq and works on every collection.
Use Find to get the first matching item, FindAll for all of them, or FindIndex for its position.