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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With