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# Average Method
The Average() method in C# calculates the arithmetic mean of a sequence of numeric values. This method is part of LINQ (Language Integrated Query) and can be used with arrays, lists, and other enumerable collections.
The Average() method has multiple overloads to work with different numeric types including int, double, decimal, float, and their nullable counterparts.
Syntax
Following is the basic syntax for the Average() method −
public static double Average(this IEnumerable<int> source) public static double Average(this IEnumerable<double> source) public static decimal Average(this IEnumerable<decimal> source)
Parameters
-
source − An enumerable sequence of numeric values to calculate the average of.
Return Value
Returns the average of the sequence as a double, decimal, or float depending on the input type. For integer inputs, it returns a double.
Using Average() with Integer Arrays
Example
using System;
using System.Linq;
class Demo {
static void Main() {
var arr = new int[] { 10, 17, 25, 30, 40, 55, 60, 70 };
double avg = arr.Average();
Console.WriteLine("Array: [" + string.Join(", ", arr) + "]");
Console.WriteLine("Average = " + avg);
}
}
The output of the above code is −
Array: [10, 17, 25, 30, 40, 55, 60, 70] Average = 38.375
Using Average() with Different Data Types
Example
using System;
using System.Linq;
using System.Collections.Generic;
class Program {
static void Main() {
// Double array
double[] doubleArr = { 1.5, 2.7, 3.2, 4.8, 5.1 };
double doubleAvg = doubleArr.Average();
Console.WriteLine("Double Average: " + doubleAvg);
// Decimal array
decimal[] decimalArr = { 10.5m, 20.3m, 30.7m };
decimal decimalAvg = decimalArr.Average();
Console.WriteLine("Decimal Average: " + decimalAvg);
// List of integers
List<int> numbers = new List<int> { 5, 15, 25, 35 };
double listAvg = numbers.Average();
Console.WriteLine("List Average: " + listAvg);
}
}
The output of the above code is −
Double Average: 3.46 Decimal Average: 20.5 List Average: 20
Using Average() with Conditional Logic
You can combine Average() with LINQ Where() to calculate averages based on conditions −
Example
using System;
using System.Linq;
class Program {
static void Main() {
int[] scores = { 85, 92, 78, 96, 88, 73, 91, 82 };
// Average of all scores
double overallAvg = scores.Average();
Console.WriteLine("Overall Average: " + overallAvg);
// Average of scores above 80
double highScoresAvg = scores.Where(x => x > 80).Average();
Console.WriteLine("Average of scores above 80: " + highScoresAvg);
// Count and average of passing scores (75+)
var passingScores = scores.Where(x => x >= 75);
Console.WriteLine("Passing scores count: " + passingScores.Count());
Console.WriteLine("Passing scores average: " + passingScores.Average());
}
}
The output of the above code is −
Overall Average: 85.625 Average of scores above 80: 88.8333333333333 Passing scores count: 7 Passing scores average: 87.8571428571429
Comparison of Average Calculation Methods
| Method | Usage | Return Type |
|---|---|---|
| arr.Average() | Direct LINQ extension method | double (for int input) |
| Queryable.Average(arr.AsQueryable()) | Queryable version for complex queries | double (for int input) |
| Manual calculation | arr.Sum() / arr.Length | Depends on division operation |
Conclusion
The Average() method in C# provides a simple and efficient way to calculate the arithmetic mean of numeric collections. It works seamlessly with LINQ operations and supports various numeric types, making it essential for statistical calculations and data analysis in .NET applications.
