© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•11mo ago•
4 replies
Bored Student

✅ 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?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

yield return
C#CC# / help
2y ago
Yield return
C#CC# / help
4y ago
yield return in c#
C#CC# / help
2y ago
enumerable.range vs yield return
C#CC# / help
2y ago