繁体   English   中英

如何返回一个空的 IEnumerable?

[英]How can I return an empty 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
            });
        }

由于所有内容都在 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>();
            }
        }

上述方法行不通,实际上不应该; 我只是觉得它说明了我的意图。 我觉得我应该指定代码不起作用,因为您无法创建抽象类的实例。

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

        }

感谢您的时间。

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

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

对我来说,最优雅的方式是yield break

这当然只是个人喜好的问题,但我会使用 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
                }
        }
    }
}

我认为最简单的方法是

 return new Friend[0];

返回的要求仅仅是该方法返回一个实现IEnumerable<Friend>的对象。 在不同情况下返回两种不同类型的对象是无关紧要的,只要它们都实现了 IEnumerable。

public IEnumerable<Friend> FindFriends()
{
    return userExists ? 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
        }): new List<Friend>();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM