array contains c# code example
Example 1: c# arraylist contains
IList arList = new ArrayList();
arList.Add(100);
arList.Add("Hello World");
arList.Add(300);
Console.WriteLine(arList.Contains(100));
Example 2: c# check if string is in array
using System;
namespace Example
{
class Program
{
static void Main(string[] args)
{
string[] planets = { "Mercury", "Venus",
"Earth", "Mars", "Jupiter",
"Saturn", "Uranus", "Neptune" };
if (planets.Contains("Jupiter"))
{
Console.WriteLine("It contains Jupiter");
}
Console.WriteLine("One or more planets begin with 'M': {0}",
Array.Exists(planets, element => element.StartsWith("M")));
Console.WriteLine("One or more planets begin with 'T': {0}",
Array.Exists(planets, element => element.StartsWith("T")));
Console.WriteLine("Is Pluto one of the planets? {0}",
Array.Exists(planets, element => element == "Pluto"));
}
}
}
Example 3: how to know if an element is in an array c#
string[] names = "John", "Susan", "Sophie";
if (names.Contains("John")
{
Console.WriteLine("John is a name");
}
Example 4: c# string array contains
string[] array = { "cat", "dog", "perl" };
bool a = Array.Exists(array, element => element == "perl");
bool c = Array.Exists(array, element => element.StartsWith("d"));
bool d = Array.Exists(array, element => element.StartsWith("x"));
Example 5: c# check if array contains value
public static bool Contains(Array a, object val)
{
return Array.IndexOf(a, val) != -1;
}