✅ Early return with yield

How to i early return despite using yield The method goes like
IEnumerable<int> ProcessXml(string path)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
var nodes = xmlDoc.SelectNodes("//DataBlob");

if(nodes == null){
return [];
}

foreach(var dataBlob in nodes.Cast<XmlNode>())
{
var data = DataProcessor.Parse(dataBlob InnerText;
yield return DataProcessor.Process(data);
}
}
IEnumerable<int> ProcessXml(string path)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
var nodes = xmlDoc.SelectNodes("//DataBlob");

if(nodes == null){
return [];
}

foreach(var dataBlob in nodes.Cast<XmlNode>())
{
var data = DataProcessor.Parse(dataBlob InnerText;
yield return DataProcessor.Process(data);
}
}
Mixed returns aren't allowed, how would you style this? Wrap the foreach in an ≠null if ? Or eagerly evaluate rather slow data pro cessing? Is there another XML query method that returns a non-nullable collection?
4 Replies
ero
ero6d ago
yield break;
Bored Student
Bored StudentOP6d ago
Oh, I forgot 🤦 Thx
Denis
Denis6d ago
$close
MODiX
MODiX6d ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?