I created a method which accepts a parameter of type List<T>. The method then does some operations which fill this list. Single stepping in debug I can see that it does this correctly. However, when it returns from the method all the data in the list is lost.
public void Fill(List<MyClass> myclass)
{
//fill myclass
}

3 answers
That's correct. If the method doesn't return myclass or assign its value to a global- or instance variable, the data will be collected by the Garbage Collector. I recommend doing something like:
... and when using it:
answered 2 years ago by:
2499
beside this, i'd like to add the pass by reference way for the list:
and when you want to call it try calling it like this:
answered 2 years ago by:
1556
499
I tried both methods and they work just fine. Are there pros/cons to each method?
2499
Judging by the title, this is probably what he wanted - I just didn't pick up on that :) ... +1
1556
in the first method that foamy wrote, you copy the stack of the list while passing the parameter. if the list sent is empty the cons won't be that visible but if the list is filled then you are filling ur stack of duplicated data. the pros of this method as well is data safety, where you are keeping the original data safe until the return of the method. the method that uses the ref keyword will send the reference of the list and make changes directly to the list without copying the parameter values. the cons of this method is safety as well, since the data edit is applied directly.
Actually, as List<MyClass> is a reference type then, even if you pass it by value, any additions you make to it in the method should be persisted when the method returns.
What I suspect is happening is that you're inadvertently assigning a new List<MyClass> object to the 'myclass' parameter and then filling that.
This won't be persisted when the method returns because it's a different object to what was passed in.
If this suspicion is correct, then you need to fill the original List<MyClass> object, not instantiate a new one.
Incidentally, if you pass the List<MyClass> variable by reference as muster suggested and then assign a new object to it, the variable will still refer to the new object when the method returns. An 'out' parameter will work similarly except that you don't need to assign anything to the variable before it is passed in - the method itself must do this or the compiler will complain.
EDIT
This simple example illustrates the various scenarios:
answered 2 years ago by:
17279