C# 程序查找序列的和
首先,设置一个序列。
List<int> myList = new List<int> { 1, 2, 3, 4 ,5};现在使用可查询的 Sum() 方法来查找总和。
myList.AsQueryable().Sum();
示例
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("Sum of elements in a list...");
foreach (int res in myList) {
Console.WriteLine(res);
}
int sum = myList.AsQueryable().Sum();
Console.WriteLine("Sum = {0}", sum);
}
}输出
Sum of elements in a list... 1 2 3 4 5 Sum = 15
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP