DonDominic
-4
Q:

c# check if array contains value

/*Make sure to add 
using System.Linq;
*/


string[] printer = {"jupiter", "neptune", "pangea", "mercury", "sonic"};
if(printer.Contains("jupiter"))
{
    Process.Start("BLAH BLAH CODE TO ADD PRINTER VIA WINDOWS EXEC"");
}
7
string[] names = "John", "Susan", "Sophie";
if (names.Contains("John") //Using Contains you can know if a specific value is on an array
    {
    Console.WriteLine("John is a name");
    }
1
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"));
        }
    }
}
// The example displays the following output:
//       One or more planets begin with 'M': True
//       One or more planets begin with 'T': False
//       Is Pluto one of the planets? False
1
    public static bool Contains(Array a, object val)
    {
        return Array.IndexOf(a, val) != -1;
    }
0

New to Communities?

Join the community