一种计算数组中数字求和的方法. [英] A method to calculate the Summation of numbers in the array.

查看:201
本文介绍了一种计算数组中数字求和的方法.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,先生,

我有这种格式的数组int [] i = {0,1,2,3,4,5,6};

请提供一种可用于获取数组值总和的方法.


int值=(0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9);
int result =值;

谢谢.

Hello Sir,

I have an array in this format int[] i = {0,1,2,3,4,5,6};

Please help me with a method that I can use to get the sum of the values in my array.


int value=(0+1+2+3+4+5+6+7+8+9);
int result= value;

Thank you.

推荐答案

一些LINQ
A little bit of LINQ
int[] i = { 0, 1, 2, 3, 4, 5, 6 };
var sum = i.Sum();



或经典"



or "classic"

int s = 0;

foreach (var item in i)
{
    s += item;
}



这样难吗?



Is this so difficult?


除了解决方案1之外,它还可以帮助您从
In addition to the Solution 1, it might help you to understand more about Sum from How does it work in C#? - Part 3 (C# Linq in detail)[^]

Thanks :)


这篇关于一种计算数组中数字求和的方法.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆