C# Program to find the sum of a sequence

A sequence in C# is a collection of elements that can be processed using LINQ methods. To find the sum of a sequence, you can use several approaches including the Sum() method from LINQ, which provides a simple and efficient way to calculate the total.

The most common approach is using the LINQ Sum() method, which can be applied to any IEnumerable<T> collection.

Syntax

Following is the syntax for using the Sum() method −

// For numeric collections
collection.Sum();

// With selector function
collection.Sum(x => x.Property);

Using LINQ Sum() Method

The Sum() method calculates the sum of all numeric elements in a sequence −

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        List<int> myList = new List<int> { 1, 2, 3, 4, 5 };
        
        Console.WriteLine("Elements in the sequence:");
        foreach (int element in myList) {
            Console.WriteLine(element);
        }
        
        int sum = myList.Sum();
        Console.WriteLine("Sum = {0}", sum);
    }
}

The output of the above code is −

Elements in the sequence:
1
2
3
4
5
Sum = 15

Using Sum() with Arrays

The Sum() method also works with arrays and other numeric sequences −

using System;
using System.Linq;

public class ArraySumDemo {
    public static void Main() {
        int[] numbers = { 10, 20, 30, 40, 50 };
        double[] decimals = { 1.5, 2.5, 3.5, 4.5 };
        
        int intSum = numbers.Sum();
        double doubleSum = decimals.Sum();
        
        Console.WriteLine("Integer array sum: " + intSum);
        Console.WriteLine("Double array sum: " + doubleSum);
    }
}

The output of the above code is −

Integer array sum: 150
Double array sum: 12

Using Traditional Loop Approach

You can also calculate the sum using a traditional for loop approach −

using System;
using System.Collections.Generic;

public class LoopSumDemo {
    public static void Main() {
        List<int> numbers = new List<int> { 5, 10, 15, 20, 25 };
        int sum = 0;
        
        foreach (int number in numbers) {
            sum += number;
        }
        
        Console.WriteLine("Numbers: " + string.Join(", ", numbers));
        Console.WriteLine("Sum using loop: " + sum);
    }
}

The output of the above code is −

Numbers: 5, 10, 15, 20, 25
Sum using loop: 75

Comparison of Approaches

Method Advantages Use Case
LINQ Sum() Clean, concise, functional style Simple numeric sequences
Traditional Loop More control, readable for beginners Complex calculations or conditions
Sum() with selector Works with object properties Collections of custom objects

Conclusion

The LINQ Sum() method provides the most efficient and readable way to calculate the sum of a numeric sequence in C#. For simple scenarios, it's preferred over traditional loops due to its conciseness and functional programming approach.

Updated on: 2026-03-17T07:04:35+05:30

647 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements