If there is reassignment, passing a reference type with ref changes the behaviour, e.g.:
static void ReassignArray(int[] arr)
{
arr = new int[] { 99, 98, 97 }; // This reassignment does not affect the original array
}
static void ReassignArrayWithRef(ref int[] arr)
{
arr = new int[] { 99, 98, 97 }; // This reassignment affects the original array
}
However, is there a difference when changing the object directly:
static void ChangeFirst(int[] arr) {
arr[0] = 4; // will affect the original array
}
static void ChangeFirst(ref int[] arr) {
arr[0] = 6; // will affec the original array
}
Apart from obvious performance gains for the method with the ref
keyword, is there a difference when there is no reassignment?