Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C# 목록에서 요소의 큐브를 찾는 프로그램

<시간/>

Select 메서드와 Lambda 식을 사용하여 요소의 큐브를 계산합니다.

다음은 우리의 목록입니다.

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

이제 Select() 메서드를 사용하여 큐브를 계산합니다.

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

다음은 전체 예입니다.

예시

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);
   }
}

출력

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