List<> is a mutable reference type.
When you pass the List<> method to the method, you pass a copy of the link. So you just say what List<> . This will not copy (clone) the entire contents of List<> .
When you put ( Push ) a List<> on Stack<> , what Stack<> really saves is a copy of the link to this instance of List<> . If this instance is subsequently modified, for example using .Add("asd") or .Clear() , this "mutation" will be visible if you follow the link saved by Stack<> or another link that you have from local variable. Both links "point" to the same List<> instance.
When in your code you say:
letterList.Clear(); // do not change reference, follow reference and mutate the instance it refers to
which will modify (mutate) an existing instance of List<> so that it becomes empty. This change will be visible to anyone who references this particular instance of List<> .
If you did:
letterList = new List<string>(); // create new instance, change reference to point there (reference assignment), old instance is unchanged
which "moved" the letterList link to point to a new instance of List<> . This will not affect people with other links to the "old" instance.
The name Pass By Reference Reference is misleading. It should have been link types and link passing or something like that.
source share