You have to initialize the out
parameter in the method body (that is create a new List<Task>
instance and assign it to the out
parameter):
public bool SomeMethod(out List<Task> tasks) {
var task = Task.Factory.StartNew(() => Process.Start(info);
tasks = new List<Task>() { task };
...
}
I'm using the collection initializer syntax to add the task to the list, but you could call the Add
method instead if you prefer.
You should call the method like this:
List<Task> tasks;
SomeMethod(out tasks);
var newTask = tasks[0]; // Access the task just created.
C# 7.0 has introduced new simpler syntax where you declare the variable in the call to the function with the out
parameter:
SomeMethod(out var tasks);
var newTask = tasks[0]; // Access the task just created.
As a List<T>
is passed by reference you can get rid of the out
parameter. You then have to create the list before calling the method:
public bool SomeMethod(List<Task> tasks) {
var task = Task.Factory.StartNew(() => Process.Start(info);
tasks.Add(task);
...
}
And call it like this:
var tasks = new List<Task>();
SomeMethod(tasks);
var newTask = tasks[0]; // Access the task just created.
In general it is good practice to avoid out
parameters because they can be confusing.