Passing List value as reference

Hello all,
how can I pass a value of list to a function by reference?

Example:

`
void a ()
{

    List<int> list = new List<int>();
    List.Add(1); // list[0] = 1
    changeval(ref list[0]);
    // now list[0] = 2
}

void changeval(ref int i) { i++; return; }

`

but when passing it as reference, an error occurs. With arrays it works, but I need to increase it size in runtime.

Thanks in advance and sorry my bad english

I believe in C# it’s automatically passed by reference if you just send in the list.
i.e.

ChangeValue (List<int> list, int index) {
 list[index]++;
}

Tho this way passes a copy of the reference, not the ref itself, so you could use:

ChangeValue(ref List<int> list, ref int index) {
  list[index]++;
}

Edit: Tho note, the second way will allow you to completely reassign the list and have it rememebered, so it’s less safe. The first way (sending a copy ref) is more safe if you don’t intend to reassign list. Same for index, if index can change in another thread for example, the second way may produce unpredictable results.