天天看点

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

本文翻译自:How can I return an empty IEnumerable?

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 it, if not return an IEnumerable with no values.

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

Here is the method:

这是方法:
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
            });
        }
           

Since everything is inside the return statement, I don't know how I could do this.

由于一切都在return语句中,我不知道如何做到这一点。

Would something like this work?

会这样的吗?
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>();
            }
        }
           

The above method doesn't work, and in fact it's not supposed to;

上面的方法不起作用,事实上它不应该;

I just feel it illustrates my intentions.

我觉得这说明了我的意图。

I feel I should specify that the code doesn't work because you can't create an instance of an abstract class.

我觉得我应该指定代码不起作用,因为你不能创建一个抽象类的实例。

Here is the calling code, I don't want it to receive a null IEnumerable at any time:

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

        }
           

Thank you for your time.

感谢您的时间。

#1楼

参考:https://stackoom.com/question/DYBu/我怎样才能返回一个空的IEnumerable

#2楼

You can use

list ?? Enumerable.Empty<Friend>()

你可以使用

list ?? Enumerable.Empty<Friend>()

list ?? Enumerable.Empty<Friend>()

, or have

FindFriends

return

Enumerable.Empty<Friend>()

list ?? Enumerable.Empty<Friend>()

,或让

FindFriends

返回

Enumerable.Empty<Friend>()

#3楼

您可以返回

Enumerable.Empty<T>()

#4楼

至于我,最优雅的方式是

yield break

#5楼

I think the simplest way would be

我认为最简单的方法是
return new Friend[0];
           

The requirements of the return are merely that the method return an object which implements

IEnumerable<Friend>

.

返回的要求仅仅是该方法返回一个实现

IEnumerable<Friend>

的对象。

The fact that under different circumstances you return two different kinds of objects is irrelevant, as long as both implement IEnumerable.

事实上,在不同情况下你返回两种不同类型的对象是无关紧要的,只要两者都实现IEnumerable。

#6楼

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

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