I correctly understood that the following fragment does not work (the elements of the array are not changed), because the array has an integer, which is a value type.
class Program
{
public static void Main()
{
int[] ints = new int[] { 1,2 };
Array.ForEach(ints, new Action<int>(AddTen));
}
static void AddTen(int i)
{
i+=10;
}
}
The same applies if the example used a string array, presumably because the string is immutable.
I have a question: -
Is there any way around this? I cannot change the signature of the callback method - for example, by adding the ref keyword, and I do not want the value type to be associated with the class - which would work ...
(Of course, I could just write an old-fashioned foreach loop to do this!)
bjg
source
share