Sam Sam - 3 months ago 12
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.Add(4,5,3)
array minus 1;
for (int z = 0; z < N; z++)
{
Console.WriteLine(array[z]);
}
Console.ReadLine();


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

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
Console.WriteLine(String.Join(",",ListInt));
Comments