Am I correct in thinking the following snippet does not work (the array items are not modified) because the array is of integer which is value type.
class Program
{
public static void Main()
{
int[] ints = new int[] { 1,2 };
Array.ForEach(ints, new Action<int>(AddTen));
// ints is not modified
}
static void AddTen(int i)
{
i+=10;
}
}
The same would apply if the example used an array of string, presumably because string is immutable.
The question I have is:-
Is there a way around this? I can't change the signature of the callback method - for instance by adding the ref keyword and I don't want to wrap the value type with a class - which would work...
(Of course, I could simply write an old fashioned foreach loop to do this!)
You are simply changing the local variable - not the item in the list. To do this, you want ConvertAll, which creates a new list/array:
int[] ints = new int[] { 1,2 };
int[] newArr = Array.ConvertAll<int,int>(ints, AddTen);
with:
static int AddTen(int i)
{
return i+10;
}
This can also be written with an anonymous method:
int[] newArr = Array.ConvertAll<int,int>(ints,
delegate (int i) { return i+ 10;});
Or in C# 3.0 as a lambda:
int[] newArr = Array.ConvertAll(ints, i=>i+10);
Other than that... a for loop:
for(int i = 0 ; i < arr.Length ; i++) {
arr[i] = AddTen(arr[i]); // or more directly...
}
But one way or another, you are going to have to get a value out of your method. Unless you change the signature, you can't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With