C# Program to Check the Length of Courses is More than 2 Characters Using LINQ
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#
using System;
using System.Linq;
class GFG{
static void Main( string [] args)
{
string [] course1 = { "cse" , "ece" , "bio-tech" , "chemical" , "civil" };
string [] course2 = { "DSA" , "JS" , "Kotlin" , "C" , "React" };
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
Share your thoughts in the comments
Please Login to comment...