首页 文章

我怎样才能返回一个空的IEnumerable?

提问于
浏览
274

鉴于以下代码和in this question给出的建议,我决定修改这个原始方法并询问IEnumarable中是否有任何值返回它,如果没有返回没有值的IEnumerable .

这是方法:

public IEnumerable<Friend> FindFriends()
        {
            //Many thanks to Rex-M for his help with this one.
            //https://stackoverflow.com/users/67/rex-m

            return doc.Descendants("user").Select(user => new Friend
            {
                ID = user.Element("id").Value,
                Name = user.Element("name").Value,
                URL = user.Element("url").Value,
                Photo = user.Element("photo").Value
            });
        }

由于一切都在return语句中,我不知道如何做到这一点 . 这样的事情会起作用吗?

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

上述方法不应该是不应该的;我觉得这说明了我的意图 . 我觉得我应该指定代码没有创建抽象类的实例 .

这是调用代码,我不希望它随时接收null IEnumerable:

private void SetUserFriends(IEnumerable<Friend> list)
        {
            int x = 40;
            int y = 3;


            foreach (Friend friend in list)
            {
                FriendControl control = new FriendControl();
                control.ID = friend.ID;
                control.URL = friend.URL;
                control.SetID(friend.ID);
                control.SetName(friend.Name);
                control.SetImage(friend.Photo);

                control.Location = new Point(x, y);
                panel2.Controls.Add(control);

                y = y + control.Height + 4;
            } 

        }

感谢您的时间 .

5 回答

  • 1

    你可以使用 list ?? Enumerable.Empty<Friend>() ,或者 FindFriends 返回 Enumerable.Empty<Friend>()

  • 84

    你可以返回Enumerable.Empty<T>() .

  • 133

    至于我,最优雅的方式是 yield break

  • 479

    这当然只是个人偏好的问题,但我会使用yield return来编写这个函数:

    public IEnumerable<Friend> 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
                    }
            }
        }
    }
    
  • 8

    我认为最简单的方法是

    return new Friend[0];
    

    返回的要求仅仅是该方法返回一个实现 IEnumerable<Friend> 的对象 . 事实上,在不同的情况下,你返回两种不同的对象是无关紧要的,只要两者都实现IEnumerable .

相关问题