Problem
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.
Here’s how to do it:
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
});
}
I’m not sure how I could achieve this because everything is contained within the return statement. Would something similar to this be feasible?
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 method described above doesn’t work, and it’s not designed to; I’m just using it to demonstrate my goals. I feel compelled to point out that the code fails because an abstract class cannot be instantiated.
I don’t want it to ever receive a null IEnumerable, so here’s the calling code:
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 taking the time to read this.
Asked by Sergio Tapia
Solution #1
You may make use of a list?? Enumerable. Make FindFriends return Enumerable or use EmptyFriend>(). Empty()
Answered by Michael Mrozek
Solution #2
You have the option of returning Enumerable. Empty().
Answered by LukeH
Solution #3
The most elegant method, in my opinion, is yield break.
Answered by Pavel Tupitsyn
Solution #4
Of course, this is purely a question of taste, but I’d write this function using 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
}
}
}
}
Answered by Chaos
Solution #5
The easiest method, in my opinion, would be
return new Friend[0];
The method must return an object that implements IEnumerableFriend> to meet the return criteria. It doesn’t matter if you return two different sorts of objects under different circumstances as long as both implement IEnumerable.
Answered by James Curran
Post is based on https://stackoverflow.com/questions/3229698/how-can-i-return-an-empty-ienumerable