C# sum question

I ones saw something like this in C# which somehow works. They had a list of values and instead of doing a for-loop they just used the + operator which I cannot do somehow.

So, do you might know how it could have been written, adding summing up one value to all other values in the list :slight_smile:

System.Linq :upside_down_face: :upside_down_face: :upside_down_face: :upside_down_face:
Enumerable.Sum Method

I am getting crazy about it (for reference see profile picture).

List<int> aInts = new List<int>{1,2,4,4,5,9,9};
aInts += 2;


question4.gh (3.9 KB)


int sum = list.Sum();

Or use the long way round:

int sum = 0;
foreach(int i in list) sum+=i;

What I am trying to do is


Returning a new list without a for-loop :slight_smile:

List<int> aInts = new List<int>{1,2,4,4,5,9,9};
aInts += 2;

A = aInts;

That is not what Enumerable.Sum does.

Here are numerous examples on how to archive your goal: https://stackoverflow.com/questions/37030075/how-to-minus-something-from-every-value-in-array-c-sharp

1 Like
list = list.Select(c => {c +=2; return c;}).ToList();
1 Like

I have to say I don’t get it. You have to go through each element in the memory anyway, so what is the good reason for avoiding a for loop?

for (int i = 0; i < aInts.Count; i++) aInts[i]+=2;

1 Like


list=(from val in list select val+2).ToList();


list=[val+2 for val in list]

to write it shorter :slight_smile:

Sledgehammers and nuts spring to mind :wink:


(sorry, different topic but…) Why does this code even work? aInts[i] is just an int, not a reference…

1 Like


List<int> numberInts = new List<int>{1,1,1,2,2,3,4,4,5};
int addingIn = 2;

List<int> aNewInts = numberInts.Select(v => v + addingIn).ToList();
A = aNewInts;

List<int> bEmptyInts = new List<int>();
foreach(int i in numberInts){
  bEmptyInts.Add(numberInts[i] + addingIn);
B = bEmptyInts;

List<int> cEmptyInts = new List<int>();
for(int i = 0;i < numberInts.Count;i++){
  cEmptyInts.Add(numberInts[i] + addingIn);
C = cEmptyInts;

List<int> dNewInts = numberInts;
for(int i = 0;i < dNewInts.Count;i++){
  dNewInts[i] = dNewInts[i] + addingIn;
D = dNewInts;

//E problem
//List<int> eNewInts = numberInts;
//eNewInts.ForEach(v => (v + addingIn));
//E = eNewInts;

var fNewInts = numberInts.Select(v => v + addingIn);
F = fNewInts;

List <int> gNewInts = numberInts.Select(v => {v += addingIn; return v;}).ToList();
G = gNewInts;

List<int> hNewInts = numberInts;
for (int i = 0; i < hNewInts.Count; i++) hNewInts[i] += addingIn;
H = hNewInts;

List<int> iNewInts = numberInts;
iNewInts = (from v in iNewInts select v + addingIn).ToList();

question4b.gh (5.1 KB)

Are we being trolled here?

Who you talking about? I still cannot comprehend the word ‘trolling.’

You tell us you would like to write the code as short as possible, we try and help you, then you post a huge chunk of code that doesn’t relate to the original question without any explanation. What’s going on?

Someone is super-sugared.

1 Like

It is a summary of all the ways to sum a value to all values in a list.
A summary of all the information you provided me :slight_smile:

What you mean by super-sugared? Hyper-active?
I already have difficulty with understanding the word ‘trolled.’ So, please do not further confuse me (see my already confused profile picture face).

Almost all of them are repeated. You may have 3 forms although I think only the for loop is understood by the machine and the rest are syntactic sugar understood by the compiler, but not completely sure.

1 Like

The end result is the sum of all the sum options. Everyone gets a prize for participation, a free confused cat.

1 Like

I made it for myself but I thought I also could share it. Not that you need it because you are super-pro’s but just for if others would need it :slight_smile: