If you create a method that return a list and you don’t have data, always return empty list instead of null. This would allow the caller to iterate the list without checking. As an example, consider the 2 blocks of code below, which is better?
var customers = GetCustomers();
foreach(var customer in customers) {
// do something
}
var customers = GetCustomers();
if (customers != null) {
foreach(var customer in customers) {
// do something
}
}
Having said that, if you write caller side code and received a list. Make sure you don’t get NullReferenceException by using this code.
var customers = GetCustomers();
foreach(var customer in customers ?? Array.Empty<Customer>()) {
// do something
}