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 average of a sequence of numeric values
Use the LINQ Average() method to find the average of a sequence of numeric values. LINQ provides multiple approaches to calculate averages from collections like arrays, lists, and other enumerable sequences.
Syntax
Following is the syntax for using the Average() method −
// For IEnumerable<T> collection.Average() // For IQueryable<T> Queryable.Average(collection.AsQueryable())
Return Value
The Average() method returns a double value representing the arithmetic mean of the sequence. For integer sequences, the result is automatically converted to double to preserve decimal precision.
Using Average() with List Collections
The simplest approach is to use the Average() extension method directly on a collection −
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
List<int> numbers = new List<int> { 5, 8, 13, 35, 67 };
double avg = numbers.Average();
Console.WriteLine("Average = " + avg);
Console.WriteLine("Count = " + numbers.Count);
}
}
The output of the above code is −
Average = 25.6 Count = 5
Using Average() with Arrays
The Average() method works seamlessly with arrays of numeric types −
using System;
using System.Linq;
class Program {
static void Main() {
int[] scores = { 85, 92, 78, 96, 89 };
double[] temperatures = { 23.5, 26.8, 21.2, 29.1 };
Console.WriteLine("Average score: " + scores.Average());
Console.WriteLine("Average temperature: " + temperatures.Average());
}
}
The output of the above code is −
Average score: 88 Average temperature: 25.15
Using Queryable.Average() for IQueryable
When working with queryable data sources, use Queryable.Average() −
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
List<int> list = new List<int> { 5, 8, 13, 35, 67 };
double avg = Queryable.Average(list.AsQueryable());
Console.WriteLine("Average = " + avg);
// Alternative using Where clause
var evenAvg = list.AsQueryable().Where(x => x % 2 == 0).Average();
Console.WriteLine("Average of even numbers = " + evenAvg);
}
}
The output of the above code is −
Average = 25.6 Average of even numbers = 6.5
Comparison of Approaches
| Method | Use Case | Performance |
|---|---|---|
| collection.Average() | Simple collections (List, Array) | Fastest - direct enumeration |
| Queryable.Average() | Database queries, complex filtering | Optimized for queryable sources |
| Manual calculation | Custom logic or very large datasets | Most control, potential for optimization |
Conclusion
The LINQ Average() method provides a simple and efficient way to calculate the arithmetic mean of numeric sequences. Use the direct extension method for simple collections, or Queryable.Average() when working with queryable data sources that require complex filtering or database integration.
