Here is a really short and simple example of how multiply all elements from a list with C#. The bad way and the good way.

The bad way to multiply items from a list with C#

using System;
using System.Collections.Generic;

public class Program 
{
   public static void Main() 
   {
      List<int> myList = new List<int>() { 5, 10, 15 };
      
      int mult = 1;

      foreach(int i in myList)
         mult = mult * i;
      
      Console.WriteLine(mult);
   }
}
Code language: C# (cs)

There is actually nothing bad about it. It is just not sexy and doesn’t use LINQ. But to be honest with you, it is probably the most instinctive way to write it.

So like I said, why not using the power of LINQ? This code will give you the same result.

The good way to multiply numbers from a list

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

public class Program 
{
   public static void Main() 
   {
      List<int> myList = new List<int>() { 5, 10, 15 };
      var mult = myList.Aggregate((x, y) => x * y);
      Console.WriteLine(mult);
   }
}
Code language: C# (cs)

Now let’s say you need to do more than just multiplying. Something like A² * B² * C², then you could still use the power of LINQ by doing like this:

var mult = myList
      .Select(x => Math.Pow(x, 2))
      .Aggregate((x, y) => x * y);
Code language: C# (cs)

Happy coding! 🙂