Sam Sam - 1 year ago 95
C# Question

How to minus something from every value in array c#

I would like to ask how to minus some value from all values in an array c#?

List<int> array = new List<int>();
array minus 1;
for (int z = 0; z < N; z++)

In output I would like to have this: 3,4,2

Actually I would like to do it in a way that I can work with the changed array not just to print out array minus 1.

Answer Source

I would like to rename the variable first, a list of integers with a name array is not good. so let me call the variable as ListInt. instead of using -1 it is better to use a variable called someValue. now see the code and how it works:

List<int> ListInt = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int someValue = -1;
ListInt = ListInt.Select(x => x - someValue).ToList();
// now the ListInt contains all the values as required
// Print the values using
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download