C#
This post will discuss how to check if all items are the same in a non-empty list in C#.
1. Using Enumerable.Distinct
Method
A simple solution to check if all list items have the same value is to get the distinct elements count in the list using LINQ’s Enumerable.Distinct
method. If all elements in the list are the same, then the distinct count would be 1. This is demonstrated below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> nums = new List<int>() { 1, 1, 1, 1, 1 }; bool isAllEqual = nums.Distinct().Count() == 1; Console.WriteLine(isAllEqual);// True } } |
You can also skip the first element after getting the distinct elements from a sequence and check if there are any elements left, as the following example illustrates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> nums = new List<int>() { 1, 1, 1, 1, 1 }; bool isAllEqual =nums.Count > 0 && !nums.Distinct().Skip(1).Any(); Console.WriteLine(isAllEqual);// True } } |
2. Using Enumerable.Any
Method
Another option to check if all list elements are identical is to compare each element of the list with its first element. This can be easily done using LINQ’s Enumerable.Any() method that returns true
if and only if any element of a sequence satisfy a condition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> nums = new List<int>() { 1, 1, 1, 1, 1 }; bool isAllEqual = nums.Count > 0 && !nums.Any(x => x != nums.First()); Console.WriteLine(isAllEqual);// True } } |
Alternatively, you can use the Enumerable.All
method that returns true
if all elements of a sequence match the conditions defined by the supplied predicate, false
otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> nums = new List<int>() { 1, 1, 1, 1, 1 }; bool isAllEqual = nums.Count > 0 && nums.All(x => x == nums.First()); Console.WriteLine(isAllEqual);// True } } |
3. Using List<T>.TrueForAll
Method
If you don’t prefer LINQ or do not use .NET 3.5 or above, you can use the List<T>.TrueForAll method that returns true
if every element in the list satisfies the supplied predicate. Here’s an example of its usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> nums = new List<int>() { 1, 1, 1, 1, 1 }; bool isAllEqual = nums.TrueForAll(x => x.Equals(nums.First())); Console.WriteLine(isAllEqual);// True } } |
That’s all about checking if all items are the same in a List in C#.
Also See:
Rate this post
Average rating 5/5. Vote count: 15
No votes so far! Be the first to rate this post.
We are sorry that this post was not useful for you!
Tell us how we can improve this post?
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)
Software Engineer | Content Writer | 12+ years experience