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 find the smallest element from an array using Lambda Expressions
Lambda expressions in C# provide a concise way to write anonymous functions. When combined with LINQ methods like Min(), they offer powerful tools for array operations. This article demonstrates how to find the smallest element from an array using lambda expressions.
Syntax
The basic syntax for using Min() with lambda expressions −
array.Min() // finds minimum value directly array.Min(element => expression) // finds minimum based on expression
Using Min() Without Lambda Expression
For simple cases, you can find the smallest element directly using the Min() method −
using System;
using System.Linq;
class Program {
static void Main() {
int[] arr = { 10, 15, 5, 20 };
int smallest = arr.Min();
Console.WriteLine("Smallest element: " + smallest);
}
}
The output of the above code is −
Smallest element: 5
Using Min() with Lambda Expression
Lambda expressions become useful when you need to apply a transformation or condition before finding the minimum. Here's an example that finds the smallest absolute value −
using System;
using System.Linq;
class Program {
static void Main() {
int[] arr = { -10, 15, -3, 20 };
int smallestAbs = arr.Min(element => Math.Abs(element));
Console.WriteLine("Smallest absolute value: " + smallestAbs);
// Find the actual element with smallest absolute value
int actualElement = arr.Where(x => Math.Abs(x) == smallestAbs).First();
Console.WriteLine("Element with smallest absolute value: " + actualElement);
}
}
The output of the above code is −
Smallest absolute value: 3 Element with smallest absolute value: -3
Finding Minimum from Complex Objects
Lambda expressions are particularly useful when working with arrays of custom objects −
using System;
using System.Linq;
class Student {
public string Name { get; set; }
public int Age { get; set; }
public double Grade { get; set; }
}
class Program {
static void Main() {
Student[] students = {
new Student { Name = "Alice", Age = 20, Grade = 85.5 },
new Student { Name = "Bob", Age = 22, Grade = 78.2 },
new Student { Name = "Charlie", Age = 19, Grade = 92.1 }
};
double lowestGrade = students.Min(student => student.Grade);
int youngestAge = students.Min(student => student.Age);
Console.WriteLine("Lowest grade: " + lowestGrade);
Console.WriteLine("Youngest age: " + youngestAge);
}
}
The output of the above code is −
Lowest grade: 78.2 Youngest age: 19
Comparison of Approaches
| Method | Use Case | Syntax |
|---|---|---|
| arr.Min() | Direct minimum value | Simple, no lambda needed |
| arr.Min(x => expression) | Minimum based on transformation | Lambda expression required |
| arr.Where().Min() | Minimum from filtered elements | Combined with filtering |
Conclusion
Lambda expressions with the Min() method provide a powerful and concise way to find the smallest element in arrays. They are especially useful when you need to apply transformations or work with complex objects, making your code more readable and efficient.
