Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I return an empty IEnumerable?

Tags:

c#

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.

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. 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:

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.

like image 701
Sergio Tapia Avatar asked Jul 12 '10 15:07

Sergio Tapia


People also ask

How do I return an IEnumerable type in C#?

IEnumerable has just one method called GetEnumerator. This method returns another type which is an interface that interface is IEnumerator. If we want to implement enumerator logic in any collection class, it needs to implement IEnumerable interface (either generic or non-generic).

Can IEnumerable where return null?

The returned IEnumerable<> might be empty, but it will never be null .

How do I initialize an IEnumerable?

public static IEnumerable<T> CreateEnumerable<T>(params T[] values) => values; //And then use it IEnumerable<string> myStrings = CreateEnumerable("first item", "second item");//etc..


3 Answers

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

This can be found under the System.Linq namespace.

like image 51
Michael Mrozek Avatar answered Oct 17 '22 01:10

Michael Mrozek


You could return Enumerable.Empty<T>().

like image 188
LukeH Avatar answered Oct 17 '22 02:10

LukeH


As for me, most elegant way is yield break

like image 116
Pavel Tupitsyn Avatar answered Oct 17 '22 00:10

Pavel Tupitsyn