Open In App

C# Program to Check the Length of Courses is More than 2 Characters Using LINQ

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array that contains a list of different courses like “DSA”, “OS”, “JavaScript”, etc., now our task is to check the length of all the courses present in the given array is more than 2 characters or not with the help of LINQ. So to do our task we use the All() method of LINQ. This method returns true if all the elements present in the given sequence satisfy the specified condition. Otherwise, it will return false.

Syntax:

array_name.All(iterator => iterator.Length > 2)

Where array_name is the input array and Length is the function used to check the length of each course.

Example:

Input  : { "cse", "it", "ft", "bio-tech", "chemical" }
Output : False

Input  : { "cse", "ece", "bio-tech", "chemical" }
Output : True

C#




// C# program to find the check the length of
// courses is more than 2 characters Using LINQ
using System;
using System.Linq;
  
class GFG{
  
static void Main(string[] args)
{
      
    // Create 5 courses
    string[] course1 = { "cse", "ece", "bio-tech", "chemical", "civil" };
    string[] course2 = { "DSA", "JS", "Kotlin", "C", "React" };
      
    // Checking the length of all courses 
    // is greater than 2 or not
    bool result1 = course1.All(display => display.Length > 2);
    bool result2 = course2.All(display => display.Length > 2);
      
    Console.WriteLine("Is the length of the courses " +
                      "is greater than 2 in course1?: " + result1);
                        
    Console.WriteLine("Is the length of the courses " +
                      "is greater than 2 in course2?: " + result2);
}
}


Output:

Is the length of the courses is greater than 2 in course1?: True
Is the length of the courses is greater than 2 in course2?: False

Last Updated : 28 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads