How to search an element in array in C#

0

 


Searching for an element in an array is a common task in programming. In C#, you can search an array for a specific element using different methods such as the Array.IndexOf method, the Linq method Contains, or by using a loop to manually iterate through the array.


Using Array.IndexOf :

Here's an example of how you can use the Array.IndexOf method to search for an element in an array in C#:



using System;

namespace ArraySearch
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 1, 2, 3, 4, 5 };
            int searchValue = 3;
            int index = Array.IndexOf(numbers, searchValue);
            if (index == -1)
            {
                Console.WriteLine("The value was not found in the array.");
            }
            else
            {
                Console.WriteLine("The value was found at index {0}.", index);
            }
        }
    }
}


In this example, we declared an array of integers named numbers and a variable searchValue equal to 3. We then used the Array.IndexOf method to search the numbers array for the value 3, and the method returned the index of the first occurrence of the value in the array. If the value is not found in the array, the Array.IndexOf method returns -1.


Using Linq method Contains :

Here's another example that demonstrates how you can use the Linq method Contains to search for an element in an array:


using System;
using System.Linq;

namespace ArraySearch
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 1, 2, 3, 4, 5 };
            int searchValue = 3;
            bool isFound = numbers.Contains(searchValue);
            if (isFound)
            {
                Console.WriteLine("The value was found in the array.");
            }
            else
            {
                Console.WriteLine("The value was not found in the array.");
            }
        }
    }
}

In this example, we declared an array of integers named numbers and a variable searchValue equal to 3. We then used the Linq method Contains to search the numbers array for the value 3. The method returns a bool value that indicates whether the value was found in the array or not.


Using Loops :

Finally, here's an example of how you can use a loop to manually search for an element in an array:


using System;

namespace ArraySearch
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 1, 2, 3, 4, 5 };
            int searchValue = 3;
            int index = -1;
            for (int i = 0; i < numbers.Length; i++)
            {
                if (numbers[i] == searchValue)
                {
                    index = i;
                    break;
                }
            }
            if (index == -1)
            {
                Console.WriteLine("The value was not found in the array.");
            }
            else
            {
                Console.WriteLine("The value was found at index {0}.", index);
            }
        }
    }
}


In this example, we declared an array of integers named numbers and a variable searchValue equal to 3. We then used a for loop to iterate through the numbers array and compare each element with the searchValue. If the element is equal to the searchValue, we break the loop and assign the index to the index variable. If the value is not found in the array, the index variable remains -1.

In conclusion, there are multiple ways to search for an element in an array in C#, including using the Array.IndexOf method, the Linq method Contains, or by using a loop. Choose the method that best fits your needs and requirements.

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !