Skip to main content

How to Check Whether an Array Contains a Value in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Is this size one we sell? Is this choice one of the allowed ones? In C#, LINQ's Contains answers that for an array: true when the array holds the value, false when it doesn't.

Syntax​

Syntax
bool <found> = <array>.Contains(<value>);
bool <found> = <array>.Contains(<value>, StringComparer.OrdinalIgnoreCase);
PartWhat it does
<array>.Contains(<value>)true when one of the items equals the value. For strings, case counts
StringComparer.OrdinalIgnoreCaseA second argument that makes the check ignore case

Microsoft's Enumerable.Contains reference: "Determines whether a sequence contains a specified element by using the default equality comparer."

The Example​

A shop sells three sizes, S, M and L. The program checks a size it sells, one it doesn't, and a lowercase m, first as is and then ignoring case.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which prints whether the sizes S, M and L contain M, XL and m, and whether they contain m with StringComparer.OrdinalIgnoreCase.

main.cs
string[] sizes = ["S", "M", "L"];

Console.WriteLine(sizes.Contains("M"));
Console.WriteLine(sizes.Contains("XL"));
Console.WriteLine(sizes.Contains("m"));
Console.WriteLine(sizes.Contains("m", StringComparer.OrdinalIgnoreCase));

Step 2: Run It​

Terminal
dotnet run main.cs
Output
True
False
False
True

A zsh terminal where dotnet run main.cs prints True, False, False and True.

  1. M is one of the sizes: True.
  2. XL isn't: False.
  3. m isn't either, because string comparison counts case: False.
  4. With StringComparer.OrdinalIgnoreCase, m matched M: True.

The command is the same in zsh, bash and PowerShell.

Not the Same as string.Contains​

A string has a Contains too, but it looks for text inside the string. Keep choices in an array, not in one string:

main.cs
string sizes = "S,M,XL";
string[] sizeList = ["S", "M", "XL"];

Console.WriteLine(sizes.Contains("L"));
Console.WriteLine(sizeList.Contains("L"));
Output
True
False

The string said True for L, because XL has an L in it. The array compared whole items, so L wasn't there.

null Gives False​

Contains with null doesn't throw for an array of strings. It says false, because no item is null:

main.cs
string[] sizes = ["S", "M", "L"];
string? picked = null;

Console.WriteLine(sizes.Contains(picked));
Output
False

Common Mistakes​

MistakeWhat happensDo this instead
Checking m in an array of M, L and SFalse, because case countsPass StringComparer.OrdinalIgnoreCase
Keeping the choices in one string, such as "S,M,XL"Contains("L") is True because of XLKeep them in an array

Troubleshooting​

SymptomLikely causeFix
False for a value you can see in the arrayDifferent case, or a space around itIgnore case, or trim the value
True for a value that isn't a choiceContains ran on a string, not on an arrayCheck the variable's type

FAQ​

Do I need a using directive?​

Not in a file run with dotnet run: Contains on an array comes from System.Linq, which is included by default, so the example has no using line.

Does it work on a List?​

Yes. List<string> has its own Contains: a list of S, M and L printed True for M.

Conclusion​

array.Contains(value) returns true when one of the items equals the value, and false otherwise, null included. For strings, case counts unless you pass StringComparer.OrdinalIgnoreCase, and a string's own Contains looks for text inside it instead.

Additional Resources​