How can I return an empty IEnumerable?

前端 未结 6 745
说谎
说谎 2020-12-12 12:14

Given the following code and the suggestions given in this question, I\'ve decided to modify this original method and ask if there are any values in the IEnumarable return i

6条回答
  •  长情又很酷
    2020-12-12 13:00

    That's of course only a matter of personal preference, but I'd write this function using yield return:

    public IEnumerable FindFriends()
    {
        //Many thanks to Rex-M for his help with this one.
        //http://stackoverflow.com/users/67/rex-m
        if (userExists)
        {
            foreach(var user in doc.Descendants("user"))
            {
                yield return new Friend
                    {
                        ID = user.Element("id").Value,
                        Name = user.Element("name").Value,
                        URL = user.Element("url").Value,
                        Photo = user.Element("photo").Value
                    }
            }
        }
    }
    

提交回复
热议问题