用 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP