method parameters: when to use "out" with a List parameter...

O

Ojas

I'm wondering if there is a best practice for when you have a C#
method parameter that is a List object that is loaded by the method.
It basically is an "out" parameter. Normally I would have the caller
allocate the List object and pass it in such as example 1. However,
if I use example 2, where I declare it an "out" then that has its own
advantages.

EXAMPLE 1:

void mymethod(List<string> abc)
{
If (abc == null)
{
// throw error
}

abc.Clear();

abc.Add("hello");
abc.Add("world");
}

====

versus

=====

EXAMPLE 2:

void mymethod(out List<string> abc)
{
abc = new List<string>();

abc.Add("hello");
abc.Add("world");
}
 
G

Göran Andersson

Ojas said:
I'm wondering if there is a best practice for when you have a C#
method parameter that is a List object that is loaded by the method.
It basically is an "out" parameter. Normally I would have the caller
allocate the List object and pass it in such as example 1. However,
if I use example 2, where I declare it an "out" then that has its own
advantages.

EXAMPLE 1:

void mymethod(List<string> abc)
{
If (abc == null)
{
// throw error
}

abc.Clear();

abc.Add("hello");
abc.Add("world");
}

====

versus

=====

EXAMPLE 2:

void mymethod(out List<string> abc)
{
abc = new List<string>();

abc.Add("hello");
abc.Add("world");
}

Generally avoid ref and out parameters if you can.

The obvious choise here would be to use the return value:

List<string> mymethod() {
abc = new List<string>();

abc.Add("hello");
abc.Add("world");

return abc;
}
 
G

Göran Andersson

Ojas said:
I'm wondering if there is a best practice for when you have a C#
method parameter that is a List object that is loaded by the method.
It basically is an "out" parameter. Normally I would have the caller
allocate the List object and pass it in such as example 1. However,
if I use example 2, where I declare it an "out" then that has its own
advantages.

EXAMPLE 1:

void mymethod(List<string> abc)
{
If (abc == null)
{
// throw error
}

abc.Clear();

abc.Add("hello");
abc.Add("world");
}

====

versus

=====

EXAMPLE 2:

void mymethod(out List<string> abc)
{
abc = new List<string>();

abc.Add("hello");
abc.Add("world");
}

Generally avoid ref and out parameters if you can.

The obvious choise here would be to use the return value:

List<string> mymethod() {
abc = new List<string>();

abc.Add("hello");
abc.Add("world");

return abc;
}
 
O

Ojas

Göran,

For this example, the real world usage I'm working on has multiple
"out" List objects for one method, something like...

void mymethod(out List<string> greatProducts, out List<string>
goodProducts, out List<string> okProducts, out List<string>
failingProducts);

if the method had just one list then I would likely return it rather
than use a ref/out parameter :)
 
O

Ojas

Göran,

For this example, the real world usage I'm working on has multiple
"out" List objects for one method, something like...

void mymethod(out List<string> greatProducts, out List<string>
goodProducts, out List<string> okProducts, out List<string>
failingProducts);

if the method had just one list then I would likely return it rather
than use a ref/out parameter :)
 
S

Stephany Young

Simply return a List<List<string>> object

List<List<string>> mymethod()
{
...
}


List<List<string>> _lists = mymethod();
List<string> greatProducts = _lists[0];
List<string> goodProducts = _lists[1];
List<string> okProducts = _lists[2];
List<string> failingProducts = _lists[3];


Göran,

For this example, the real world usage I'm working on has multiple
"out" List objects for one method, something like...

void mymethod(out List<string> greatProducts, out List<string>
goodProducts, out List<string> okProducts, out List<string>
failingProducts);

if the method had just one list then I would likely return it rather
than use a ref/out parameter :)
 
S

Stephany Young

Simply return a List<List<string>> object

List<List<string>> mymethod()
{
...
}


List<List<string>> _lists = mymethod();
List<string> greatProducts = _lists[0];
List<string> goodProducts = _lists[1];
List<string> okProducts = _lists[2];
List<string> failingProducts = _lists[3];


Göran,

For this example, the real world usage I'm working on has multiple
"out" List objects for one method, something like...

void mymethod(out List<string> greatProducts, out List<string>
goodProducts, out List<string> okProducts, out List<string>
failingProducts);

if the method had just one list then I would likely return it rather
than use a ref/out parameter :)
 
G

Göran Andersson

Ojas said:
Göran,

For this example, the real world usage I'm working on has multiple
"out" List objects for one method, something like...

void mymethod(out List<string> greatProducts, out List<string>
goodProducts, out List<string> okProducts, out List<string>
failingProducts);

if the method had just one list then I would likely return it rather
than use a ref/out parameter :)

Simply create a class to handle a return value with multiple values:

public class ProductsLists {

public List<string> GreatProducts { get; set; }
public List<string> GoodProducts { get; set; }
public List<string> OkProducts { get; set; }
public List<string> FailingProducts { get; set; }

}
 
G

Göran Andersson

Ojas said:
Göran,

For this example, the real world usage I'm working on has multiple
"out" List objects for one method, something like...

void mymethod(out List<string> greatProducts, out List<string>
goodProducts, out List<string> okProducts, out List<string>
failingProducts);

if the method had just one list then I would likely return it rather
than use a ref/out parameter :)

Simply create a class to handle a return value with multiple values:

public class ProductsLists {

public List<string> GreatProducts { get; set; }
public List<string> GoodProducts { get; set; }
public List<string> OkProducts { get; set; }
public List<string> FailingProducts { get; set; }

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top