check if string exists in array c# code example
Example 1: how to check if a value is inside an array c#
string[] printer = {"jupiter", "neptune", "pangea", "mercury", "sonic"};
if(printer.Contains("jupiter"))
{
Process.Start("BLAH BLAH CODE TO ADD PRINTER VIA WINDOWS EXEC"");
}
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: check if that inex exisits array c#
var array=new List<int>(1,2,3);
int count=5;
for(int i=0;i<count;i++){
if(array.Count>i){
}
}