When working with Enumerable in .Net, there is always a possibility of returning an empty Enumurable if no match is found. Consider the following code.
private IEnumarable<Book> GetBook(string name)
{
if (string.IsNullOrEmpty(name))
return Enumerable.Empty<Book>();
return _dbContext.Book.Where(b => b.Name.ToLower().Contains(name));
}
In the above piece of code Enumerable.Empty method is used to check the null name and return an empty book rather than returning null which can cause runtime exception if the name is not supplied. This is considered as defensive code practice.
Cheers,
Samitha
No comments:
Post a Comment