I want to do something like below:
total.ForEach(x => x = Math.Abs(x));
However, x is not a reference value. How can I do it?
Edit:
Is it possible to do this in place and not create another list and not use a for loop?
You can use Linq.
total.Select(x => Math.Abs(x)).ToList();
This will give you a new list of absolute values ββin total . If you want to change in place
total
for(int i = 0; i < total.Count; i++) { total[i] = Math.Abs(total[i]); }
If I understand correctly, you need a list of abs values. Try something like
List<long> a = new List<long>() { 10, -30, 40 }; //original list List<long> b = a.ConvertAll<long>(x => Math.Abs(x)); //abs list
Source: https://habr.com/ru/post/1445250/More articles:Change button text inside ListView in Android - androidSQL Server sort by first column in table - sqlhow to rotate image in php? - phpHow to claim that each item in a collection is within a range - arraysComparison of two fractions (Question mark looks new to me - javaCordoba-SQLitePlugin in PhoneGap: Uncaught TypeError cannot call the 'opendatabase' method from undefined - cordovaUsing a byte array - javaUncaught TypeError cannot call the 'opendatabase' method from an undefined -SQLite plugin with 3.5 corridor - jqueryWrap a checkmark on a label with ZF decorators - zend-frameworkAll Articles