Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<T> as 'out' parameter causes an error. Why?

In this code:

public bool SomeMethod(out List<Task> tasks)
{
    var task = Task.Factory.StartNew(() => Process.Start(info));
    tasks.Add(task);
}

I get an error, "Use of unassigned out parameter 'tasks'". Why?

In an MSDN example there's just use of out parameter

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }

    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}

Is it because of List<T>?

like image 379
Saint Avatar asked May 24 '11 13:05

Saint


People also ask

What is an out parameter?

The out parameter in C# is used to pass arguments to methods by reference. It differs from the ref keyword in that it does not require parameter variables to be initialized before they are passed to a method. The out keyword must be explicitly declared in the method's definition​ as well as in the calling method.

What is the use of out parameter in function?

The out is a keyword in C# which is used for the passing the arguments to methods as a reference type. It is generally used when a method returns multiple values.

Can an out parameter be optional?

NET framework hast many many data structures that are very flexible like List and Array and you can use them as an output parameter or as return type so there is no need to implement a way to have optional output parameters.

What is in parameter in out parameter?

Inout Parameters in Swift An inout parameter is a special type of parameter that can be modified inside a function and the changes apply outside the function.


1 Answers

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.

like image 194
Martin Liversage Avatar answered Sep 25 '22 11:09

Martin Liversage