C# - Check a specified number exists in an array using LINQ

Learn how to check a specified number exists in an array using Linq in C#? By Nidhi Last updated : April 01, 2023

Here, we will create an array of float numbers and then check a specified number is exists in an array or not using Linq Contains() method.

C# program to check a specified number exists in an array using LINQ

The source code to check a specified number exists in the array using Linq, is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to check a specified number exists 
//in an array using Linq.

using System;
using System.Linq;

class LinqDemo
{
    static void Main(string[] args)
    {
        float[] numbers = { 8.2F, 11.2F, 7.6F, 8.3F, 5.5F, 6.4F };

        bool isExist = false;

        isExist = numbers.Contains(12.5F);

        if(isExist==true)
            Console.WriteLine("Number is exist in the array");
        else
            Console.WriteLine("The number does not exist in the array");
    }
}

Output

The number does not exist in the array
Press any key to continue . . .

Explanation

In the above program, we created a class LinqDemo that contains Main() method, In the Main() method we created an array of a float number.

The Contains() method is used to check the specified number is exist in an array or not. Here, 12.5F number does not exist in the array then it will print "Number does not exist in the array" message on the console screen.

C# LINQ Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.