Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About c# lists send it or return it?

Tags:

c#

list

I have one question. I have to do a method that fills a List < object > from a sql database. My question is:

It's better to do this:

List<Product> listProducts = new List<Product>();
listProducts = loadProducts();

//this code is in other class
public List<Product> loadProducts()
{
    List<Product > listProduct = new List<Product> ();
    //code        
    return listProduct
}

Or it's better this:

List<Product> listProducts = new List<Product>();
listProducts = loadProducts(listProducts);

//this code is in other class
public List<Product> loadProducts(List<Product> listProduct)
{
    //Code        
    return listProduct
}

Sorry if it's a noob question but both of two works but I don't know which is more improved.

Thanks a lot.

like image 429
uoah Avatar asked Nov 30 '22 07:11

uoah


1 Answers

Neither, really.

You should just go like so:

 List<Product> listProducts = loadProducts();

Option 1 makes a new list only to overwrite it later. Option 2 needlessly passes a list to the method, which will just send it back out modified.

like image 184
Tejs Avatar answered Dec 06 '22 09:12

Tejs