Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to check if all the values in a list that are greater than a given value
To check if all values in a list are greater than a given value, you need to iterate through each element and compare it against the threshold. If any element fails the condition, then not all values meet the criteria.
There are multiple approaches to solve this problem in C# − using traditional loops, LINQ methods, or built-in array methods.
Using Traditional For Loop
The most straightforward approach uses a for loop to iterate through the array and check each element −
using System;
public class Program {
public static void Main(string[] args) {
int[] arr = new int[] {55, 100, 87, 45};
int threshold = 80;
bool allGreater = true;
Console.WriteLine("Array: [55, 100, 87, 45]");
Console.WriteLine("Threshold: " + threshold);
for (int i = 0; i < arr.Length; i++) {
if (arr[i] <= threshold) {
allGreater = false;
break;
}
}
Console.WriteLine("All values greater than " + threshold + ": " + allGreater);
// Show which values are greater
Console.WriteLine("Values greater than " + threshold + ":");
for (int i = 0; i < arr.Length; i++) {
if (arr[i] > threshold) {
Console.WriteLine(arr[i]);
}
}
}
}
The output of the above code is −
Array: [55, 100, 87, 45] Threshold: 80 All values greater than 80: False Values greater than 80: 100 87
Using LINQ All() Method
LINQ provides the All() method that returns true if all elements satisfy a given condition −
using System;
using System.Linq;
public class Program {
public static void Main(string[] args) {
int[] arr = new int[] {85, 100, 87, 95};
int threshold = 80;
Console.WriteLine("Array: [85, 100, 87, 95]");
Console.WriteLine("Threshold: " + threshold);
bool allGreater = arr.All(x => x > threshold);
Console.WriteLine("All values greater than " + threshold + ": " + allGreater);
// Count of elements greater than threshold
int count = arr.Count(x => x > threshold);
Console.WriteLine("Count of values greater than " + threshold + ": " + count);
}
}
The output of the above code is −
Array: [85, 100, 87, 95] Threshold: 80 All values greater than 80: True Count of values greater than 80: 4
Using Custom Method with Early Exit
A reusable method that checks the condition and exits early when a false condition is found −
using System;
public class Program {
public static bool AreAllGreater(int[] array, int threshold) {
foreach (int value in array) {
if (value <= threshold) {
return false;
}
}
return true;
}
public static void Main(string[] args) {
int[] arr1 = new int[] {55, 100, 87, 45};
int[] arr2 = new int[] {85, 100, 87, 95};
int threshold = 80;
Console.WriteLine("Testing array 1: [55, 100, 87, 45]");
Console.WriteLine("All greater than " + threshold + ": " + AreAllGreater(arr1, threshold));
Console.WriteLine("\nTesting array 2: [85, 100, 87, 95]");
Console.WriteLine("All greater than " + threshold + ": " + AreAllGreater(arr2, threshold));
}
}
The output of the above code is −
Testing array 1: [55, 100, 87, 45] All greater than 80: False Testing array 2: [85, 100, 87, 95] All greater than 80: True
Comparison of Approaches
| Approach | Advantages | Best Use Case |
|---|---|---|
| For Loop | Simple, no additional imports, full control | Basic scenarios, learning purposes |
| LINQ All() | Concise, readable, functional style | When LINQ is already used in project |
| Custom Method | Reusable, early exit optimization | Repeated operations, performance critical |
Conclusion
Checking if all values in an array are greater than a threshold can be accomplished using traditional loops, LINQ methods, or custom functions. The LINQ All() method provides the most concise solution, while traditional loops offer more control and no additional dependencies.
