C# Program to find the cube of elements in a list


Use Select method and Lambda Expression to calculate the cube of elements.

The following is our list.

List<int> list = new List<int> { 2, 4, 5, 7 };

Now, use the Select() method and calculate the cube.

list.AsQueryable().Select(c => c * c * c);

The following is the entire example.

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      List<int> list = new List<int> { 2, 4, 5, 7 };
      Console.WriteLine("Elements...");
      // initial list javascript:void(0)
      foreach (int n in list)
      Console.WriteLine(n);
      // cube of each element
      IEnumerable<int> res = list.AsQueryable().Select(c => c * c * c);
      Console.WriteLine("Cube of each element...");
      foreach (int n in res)
      Console.WriteLine(n);
   }
}

Output

Elements...
2
4
5
7
Cube of each element...
8
64
125
343

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

395 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements