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

Ser*_*pia 310 c# ienumerable

鉴于以下代码和此问题中给出的建议,我决定修改这个原始方法,并询问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
            });
        }
Run Code Online (Sandbox Code Playgroud)

由于一切都在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>();
            }
        }
Run Code Online (Sandbox Code Playgroud)

上面的方法不起作用,事实上它不应该; 我觉得这说明了我的意图.我觉得我应该指定代码不起作用,因为你不能创建一个抽象类的实例.

这是调用代码,我不希望它随时接收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;
            } 

        }
Run Code Online (Sandbox Code Playgroud)

感谢您的时间.

Mic*_*zek 544

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

  • `new List <Friend>()`是一个更昂贵的操作,因为它会创建一个列表实例(并在此过程中为它分配内存) (69认同)
  • 如果他回来说它会改变一些东西,比如说,新的List <Friend>()`因为当从那个方法返回时它会被转换为`IEnumerable <Friend>`吗? (6认同)

Pav*_*syn 97

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

  • +1正确的代码应该使用yield来处理他使用IEnumerable的方式 (14认同)
  • 但是,如果你使用收益率回报等等,不是吗? (8认同)
  • 请原谅我对这个问题的无知,但请您说明如何在这种情况下使用收益率突破?我只看过for循环中的例子,但这并没有为我画出清晰的画面. (6认同)
  • 编辑在同行评审中遭到拒绝,所以这里是我讨论的关于@Pyritie的例子 - 格式化虽然搞砸了,所以我也把它添加到http://pastebin.com/X9Z49Vq1:`public IEnumerable <Friend> FindFriends( ){if(!userExists)yield break; foreach(doc.Descendants("user")中的var descendant.选择(user => new Friend {ID = user.Element("id").Value,Name = user.Element("name").Value,URL = user.Element("url").Value,Photo = user.Element("photo").Value})){yield return descendant; } (3认同)

小智 8

这当然只是个人偏好的问题,但我会使用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
                }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)