本文翻譯自: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
}
}
}
}