In this post we are going to learn about difference between ‘All’ and ‘Any’ operator in Linq.
All operator checks whether all elements have met specific conditions or not while Any operator check whether there is any elements exist in collection or not?
So what we are waiting for Let’s take a example.
Now let’s run that example. To see how its works.
As you can see first condition returns false and this is true because the first element 1 is not greater then 2 so its true and second condition return true because its contains element.
That’s it. Hope you like. Stay tuned for more..
Difference between ‘All’ and ‘Any’ operator:
So what we are waiting for Let’s take a example.
using System; using System.Linq; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int[] intArray = {1,2,3,4,5}; bool result = intArray.All(i => i > 2); Console.WriteLine(result); result = intArray.Any(); Console.WriteLine(result); Console.ReadLine(); } } }In the above example you can see that I have created an integer array and then checked with both all and any operator. In first condition I have checked that whether elements are greater then 2 or not and same way I have checked with any operator that its contains any element or not?
Now let’s run that example. To see how its works.
As you can see first condition returns false and this is true because the first element 1 is not greater then 2 so its true and second condition return true because its contains element.
That’s it. Hope you like. Stay tuned for more..
0 comments:
Post a Comment
Your feedback is very important to me. Please provide your feedback via putting comments.