This question already has an answer here:
I write simple library wich returns List of names.
But,what i should returns if i can not find anything?
return new List<String>();
or
return null;
example:
var resultColl=FindNames(...);
This code can be used from another components and i do not want to corrupted it. If i return null- i think it is right way to check it. But, may be i should return empty list?
Thank you.
You should always return empty list. See the Guidelines for Collections.
DO NOT return null values from collection properties or from methods returning collections. Return an empty collection or an empty array instead.
null
as much as possible. it's just library should offered some default empty implementations. - zinking
Returning the empty list is more convenient for users of your function:
foreach (string name in FindNames(...))
{
Display(name);
}
Returning null forces your callers to write extra code:
an extra local variable (to avoid having to call your function twice)
List<string> names = FindNames(...);
if (names != null)
{
foreach (string name in names)
{
Display(name);
}
}
So returning an empty list is better.
I'd return Enumerable.Empty<string>()
, or if your method is supposed to return a List, do return new List<string>()
. If you have a lot of situations where you need to return an empty List, you could create a static list which is returned each time, this would stop new empty lists from having to be created each time, as pointed out by @YuvalItzchakov
.
An empty collection is better than null
since it would, in my opinion lead to cleaner code.
Enumerable.Empty<string>().ToList();
- Yuval Itzchakovnew List<string>();
- Rob♦IEnumerable<string>
, this wont compile. As he used List<string>
, i assumed that's what he's using. - Yuval ItzchakovList<string>
is expected then it would need to be converted, but then again it would make the use of Enumerable.Empty redundant. - npintistatic List<string>
would be better, performance wise. - Yuval Itzchakov
It's likely that the calling code will want to iterate the list, or do something with a List. By passing back an empty List the calling code should work fine. If you return null then the calling code will have to ensure they have a list first.
It's probably just a matter of preference, but returning an empty list gets my vote ... you're returning the thing the contract said it would return.
Returning empty collection is better from the design perspective because client code doesn't need to perform null checking. See Null Object pattern.
That depends on the semantic of your code.
If no results is an acceptable result, then you should return an empty collection.
If no results is an error condition, then return null.
I would suggest that you be consistent in what you do when you have nothing to return. This way you always know for anything that you are calling to expect the same type of response for nothing. Such as knowing that you will always return an empty set or an empty string or 0 etc.
What do you do in regards to the other library items you have?
A more detailed answer would be a Tuple<bool, List<string>>
. This is a clear solution which can be modified to include other details about your search:
var thereAreResults = foundList.Count > 0;
return new Tuple<bool, List<String>>(thereAreResults, foundList);
Count
on the returned list or more likely iterate it in a foreach
. Also you could just return Tuple.Create(thereAreReullts, foundList);
instead. - juharr
List<T>
at all. Return emptyIList<T>
or, if it is possible, emptyIEnumerable<T>
instead. - Dennis