These two methods appear to behave the same to me
public IEnumerable<string> GetNothing()
{
return Enumerable.Empty<string>();
}
public IEnumerable<string> GetLessThanNothing()
{
yield break;
}
I’ve profiled each in test scenarios and I don’t see a meaningful difference in speed, but the yield break version is slightly faster.
Are there any reasons to use one over the other? Is one easier to read than the other? Is there a behavior difference that would matter to a caller?
If you intend to always return an empty enumerable then using the
Enumerable.Empty<string>()syntax is more declarative IMHO.The performance difference here is almost certainly not significant. I would focus on readability over performance here until a profiler showed you it was a problem.