Average
In this chapter you will learn:
What is Average operator
Selector type | Result type |
---|---|
decimal | decimal |
int, long, float, double | double |
using System;//from ja v a2 s .c om
using System.Linq;
class Program
{
static void Main()
{
decimal[] numbers = { 3, 4, 8 };
decimal average = numbers.Average(); // 5 (mean value)
Console.WriteLine(average);
double avg = new int[] { 3, 4 }.Average(); // 3.5
Console.WriteLine(avg);
}
}
The output:
Average length of words
using System;/*j ava2s . co m*/
using System.Linq;
public class MainClass
{
public static void Main()
{
string[] words = { "ch", "a", "blue" };
double averageLength = words.Average(w => w.Length);
Console.WriteLine("The average word length is {0} characters.", averageLength);
}
}
Next chapter...
What you will learn in the next chapter:
Home » C# Tutorial » Linq Operators