C# Linq Contains Method

One of the methods we can use to find whether the desired element exists in the Array or Collection is the "Contains" method.

The method returns a value of true or false ie bool.

In the example below, we are trying to query whether the value "60" exists in the integer array.

namespace ConsoleApplicationTest
{
    class Program
    {
        static void Main(string[] args)
        {

            int[] numArry = { 100, 90, 80, 70, 60 };

            var myValue = (from num in numArry select num).Contains(60);

           Console.WriteLine(myValue);
            Console.ReadKey();
         
        }
    }
}

Since the value "60" is in the array, the variable "myValue" will be true.

We can also use Contains method as follows.

namespace ConsoleApplicationTest
{
    class Program
    {
        static void Main(string[] args)
        {

            int[] numArry = { 100, 90, 80, 70, 60 };

            var myValue = numArry.AsEnumerable().Contains(60);

            Console.WriteLine(myValue);
            Console.ReadKey();

        }
    }
}



You May Interest

C# Finding the Operating System Username

C# Finding the Path to the Windows System32 Folder

C# Finding the Computer Name

C# Finding the Path to My Documents Folder

C# How To Find The Average Of 10 Numbers Using A While Loop