Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to use Array.IndexOf() method to get the index of the first occurrence of a specified element in 1D Array in C#

5 Answers

0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 1, 3, 5, 4, 9 };

            Console.WriteLine(Array.IndexOf(numbers, 3)); // 1
            Console.WriteLine(Array.IndexOf(numbers, 4)); // 3
            Console.WriteLine(Array.IndexOf(numbers, 20)); // -1
        }
    }
}

/*
run:
   
1
3
-1
 
*/


answered Mar 8, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 1, 3, 5, 4, 9 };

            if (Array.IndexOf(numbers, 3) != -1)
                Console.WriteLine("Found - The number 3 is in the array");
            if (Array.IndexOf(numbers, 20) == -1)
                Console.WriteLine("Not Found - The number 20 is not in the array");
        }
    }
}

/*
run:
   
Found - The number 3 is in the array
Not Found - The number 20 is not in the array
 
*/


answered Mar 8, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] array = { "c++", "csharp", "php", "java", "vb.net", "pascal"};

            Console.WriteLine(Array.IndexOf(array, "csharp")); // 1
            Console.WriteLine(Array.IndexOf(array, "python")); // -1
        }
    }
}

/*
run:
   
1
-1
 
*/


answered Mar 8, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] array = { "c++", "csharp", "php", "java", "vb.net", "pascal"};

            if (Array.IndexOf(array, "csharp") != -1)
                Console.WriteLine("Found"); // Found
            else
                Console.WriteLine("Not Found");   
            if (Array.IndexOf(array, "python") != -1)
                Console.WriteLine("Found");
            else
                Console.WriteLine("Not Found"); // Not Found
        }
    }
}

/*
run:
   
Found
Not Found
 
*/


answered Mar 8, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] array = { "c++", "csharp", "php", "java", "vb.net", "pascal"};

            if (Array.IndexOf(array, "csharp", 0, 2) != -1) // index 0, 1, 2
                Console.WriteLine("Found"); // Found
            else
                Console.WriteLine("Not Found");

            if (Array.IndexOf(array, "csharp", 2, 4) != -1) // index 2, 3, 4
                Console.WriteLine("Found"); 
            else
                Console.WriteLine("Not Found");  // Not Found 
        }
    }
}

/*
run:
   
Found
Not Found
 
*/


answered Mar 8, 2015 by avibootz
...