Skip to main content
Journey Uncommon Logo
JourneyUncommon
mediumC#yield return generatorsSingle-choice MCQ

Why does this method fail to validate its argument at the call site, and what is the standard fix?

public IEnumerable<int> Take(IEnumerable<int> src, int n) { if (src == null) throw new ArgumentNullException(nameof(src)); int i = 0; foreach (var x in src) { if (i++ >= n) yield break; yield return x; } }