Am I misunderstanding the ref keyword? Or am I missing something else?
Yes. You're not passing the list itself to the method, but rather passing the reference to the list by reference. This lets you change the reference (the List<int>
that listWithRef
actual refers to) within the method, and have it reflect.
Without using the ref
keyword, your method can't change the reference to the list - the actual list storage mechanism is unchanged in either case.
Note that this isn't required if you just want to use the list. You can call List<int>.Add
within either method, for example, and the list will get new items added to it. Ref is only required with reference types to change the reference itself.