Check if all items are same in a List in C# | Techie Delight (2024)

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

}

}

DownloadRun Code


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

}

}

DownloadRun Code

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

}

}

DownloadRun Code


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

}

}

DownloadRun Code

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

}

}

DownloadRun Code

That’s all about checking if all items are the same in a List in C#.

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 :)


Check if all items are same in a List in C# | Techie Delight (1)

Vivek Srivastava

Software Engineer | Content Writer | 12+ years experience

Check if all items are same in a List in C# | Techie Delight (2024)
Top Articles
Latest Posts
Article information

Author: Madonna Wisozk

Last Updated:

Views: 6170

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.