tags:

views:

184

answers:

5

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 is the method:

public IEnumerable<Friend> FindFriends()
        {
            //Many thanks to Rex-M for his help with this one.
            //http://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. Would something like this work?

public IEnumerable<Friend> FindFriends()
        {
            //Many thanks to Rex-M for his help with this one.
            //http://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:

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.

+12  A: 

You can use list ?? Enumerable.Empty<Friend>(), or have FindFriends return Enumerable.Empty<Friend>()

Michael Mrozek
Would it change things if he returned, say, `new List<Friend>()` since it will be cast to `IEnumerable<Friend>` when returned from that method?
Sarah Vessels
+5  A: 

You could return Enumerable.Empty<T>.

LukeH
+4  A: 

As for me, most elegant way is "yield break"

Kefir
But that's if you use yield return and such, isn't it?
Svish
+1 as his code correctly should use yield for the way he's working with IEnumerable
Chris Marisic
Pardon my ignorance on the subject, but could you please illustrate how to use yield break in this context? I've seen examples only in for loops but that doesn't paint a clear picture for me.
Sergio Tapia
A: 

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>. The fact that under different circumstances you return two different kinds of objects is irrelevant, as long as both implement IEnumerable.

James Curran
Enumerable.Empty<T> actually returns an empty array of T (T[0]), with the advantage that the same empty array is reused. Note that this approach is not ideal for non-empty arrays, because the elements can be modified (however an array can't be resized, resizing involves creating a new instance).
Francis Gagné
A: 

That's of course only a matter of personal preference, 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
                }
        }
    }
}
Chaos