0
0
C Sharp (C#)programming~10 mins

Aggregate functions (Count, Sum, Average) in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Aggregate functions (Count, Sum, Average)
Start with a list of numbers
Apply Count function
Get total number of items
Apply Sum function
Add all numbers together
Apply Average function
Divide Sum by Count
Result output
This flow shows how we start with a list, count items, sum them, then find the average by dividing sum by count.
Execution Sample
C Sharp (C#)
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        int[] numbers = {2, 4, 6, 8};
        int count = numbers.Count();
        int sum = numbers.Sum();
        double average = numbers.Average();
        Console.WriteLine($"Count: {count}, Sum: {sum}, Average: {average}");
    }
}
This code counts, sums, and averages the numbers in the array, then prints the results.
Execution Table
StepActionValue/ResultExplanation
1Initialize array[2, 4, 6, 8]Numbers list created
2Count()4There are 4 numbers in the list
3Sum()20Sum of 2+4+6+8 equals 20
4Average()5.0Average is sum (20) divided by count (4)
5Print outputCount: 4, Sum: 20, Average: 5Final output displayed
💡 All aggregate functions applied and output printed
Variable Tracker
VariableStartAfter CountAfter SumAfter AverageFinal
numbers[2,4,6,8][2,4,6,8][2,4,6,8][2,4,6,8][2,4,6,8]
countundefined4444
sumundefinedundefined202020
averageundefinedundefinedundefined5.05.0
Key Moments - 3 Insights
Why does Average() return a double instead of an int?
Average() divides sum by count, which can result in a decimal number. So it returns a double to keep the decimal part, as shown in step 4 of the execution_table.
Does Count() count the sum of numbers or the number of items?
Count() returns the number of items in the list, not their sum. Step 2 in the execution_table shows Count() returns 4, the number of elements.
Can Sum() be used on an empty list?
Yes, Sum() on an empty list returns 0. But Average() on an empty list throws an error because it divides by zero.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'sum' after step 3?
A4
B5.0
C20
DUndefined
💡 Hint
Check the 'Value/Result' column for step 3 in execution_table
At which step does the program calculate the average?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look for the step where Average() is called in execution_table
If the array was empty, what would Count() return?
A0
BError
CNull
D1
💡 Hint
Count() returns the number of items, so empty means zero items
Concept Snapshot
Aggregate functions in C#:
- Count() returns number of items
- Sum() adds all items
- Average() divides sum by count
- Average() returns double for decimals
- Use on collections like arrays or lists
Full Transcript
This example shows how to use aggregate functions Count, Sum, and Average in C#. We start with an array of numbers. Count() tells us how many numbers are in the array. Sum() adds all the numbers together. Average() divides the sum by the count to find the average value. The code prints all three results. Average returns a double because the result can have decimals. Count returns the number of items, not their sum. Sum returns zero if the list is empty, but Average will cause an error if the list is empty because it divides by zero.