我怎樣才能返回一個空的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
                }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章