C# 如何返回空的IEnumerable?

C# 如何返回空的IEnumerable?,c#,ienumerable,C#,Ienumerable,根据下面的代码和给出的建议,我决定修改这个原始方法,并询问IEnumerable中是否有任何值返回它,如果没有,则返回一个没有值的IEnumerable 方法如下: public IEnumerable<Friend> FindFriends() { //Many thanks to Rex-M for his help with this one. //https://stackoverflow.com/users/6

根据下面的代码和给出的建议,我决定修改这个原始方法,并询问IEnumerable中是否有任何值返回它,如果没有,则返回一个没有值的IEnumerable

方法如下:

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
            });
        }
public IEnumerable FindFriends()
{
//非常感谢Rex-M在这方面的帮助。
//https://stackoverflow.com/users/67/rex-m
返回文档子体(“用户”)。选择(用户=>新朋友
{
ID=user.Element(“ID”).值,
Name=user.Element(“Name”).Value,
URL=user.Element(“URL”).Value,
Photo=user.Element(“Photo”).值
});
}
由于所有内容都在return语句中,我不知道如何才能做到这一点。你喜欢这个工作吗

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>();
            }
        }
public IEnumerable FindFriends()
{
//非常感谢Rex-M在这方面的帮助。
//https://stackoverflow.com/users/67/rex-m
如果(用户存在)
{
返回文档子体(“用户”)。选择(用户=>新朋友
{
ID=user.Element(“ID”).值,
Name=user.Element(“Name”).Value,
URL=user.Element(“URL”).Value,
Photo=user.Element(“Photo”).值
});
}
其他的
{ 
返回新的IEnumerable();
}
}
上述方法不起作用,事实上也不应该起作用;我只是觉得这说明了我的意图。我觉得我应该指定代码不起作用,因为您不能创建抽象类的实例

这是调用代码,我不希望它在任何时候接收空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;
            } 

        }
private void SetUserFriends(IEnumerable列表)
{
int x=40;
int y=3;
foreach(列表中的朋友)
{
FriendControl=新建FriendControl();
control.ID=friend.ID;
control.URL=friend.URL;
control.SetID(friend.ID);
control.SetName(friend.Name);
控件.SetImage(friend.Photo);
控制位置=新点(x,y);
面板2.控件。添加(控件);
y=y+控制。高度+4;
} 
}

谢谢您的时间。

您可以使用
列表??Enumerable.Empty()
,或拥有
FindFriends
返回
Enumerable.Empty()

您可以返回。

对我来说,最优雅的方法是
放弃

我认为最简单的方法是

 return new Friend[0];

返回的要求仅仅是方法返回一个实现
IEnumerable
的对象。在不同情况下返回两种不同类型的对象这一事实是不相关的,只要它们都实现IEnumerable。

这当然只是个人偏好的问题,但我会使用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
                }
        }
    }
}
public IEnumerable FindFriends()
{
//非常感谢Rex-M在这方面的帮助。
//http://stackoverflow.com/users/67/rex-m
如果(用户存在)
{
foreach(文档子体中的变量用户(“用户”))
{
结交新朋友
{
ID=user.Element(“ID”).值,
Name=user.Element(“Name”).Value,
URL=user.Element(“URL”).Value,
Photo=user.Element(“Photo”).值
}
}
}
}
公共IEnumerable FindFriends()
{
return userExists?doc.subscriptions(“用户”)。选择(用户=>新朋友
{
ID=user.Element(“ID”).值,
Name=user.Element(“Name”).Value,
URL=user.Element(“URL”).Value,
Photo=user.Element(“Photo”).值
}):新列表();
}

但如果你使用收益率等,不是吗?+1因为他的代码应该正确地使用收益率,因为他与IEnumerablePardon的合作方式我对这个问题一无所知,但你能说明如何在这种情况下使用收益率中断吗?我只在for循环中看到过示例,但这并不能为我描绘一幅清晰的画面。用一个示例更新了答案。我同意这是最优雅的方式编辑在同行评审中被拒绝了,所以这里是我所说的@pytiie的例子-尽管格式被弄乱了,所以我也把它添加到了:
public IEnumerable FindFriends(){if(!userExists)yield break;foreach(doc.substands(“user”)中的var substant.Select(user=>new Friend{ID=user.Element(“ID”).Value,Name=user.Element(“Name”).Value,URL=user.Element(“URL”).Value,Photo=user.Element(“Photo”).Value}{yield-return-substant;}}
查看这里的代码,您应该使用yield-return和yield-break.Enumerable.Empty实际返回一个T(T[0])的空数组,其优点是可以重用相同的空数组。请注意,这种方法对于非空数组并不理想,因为可以修改元素(但是数组无法调整大小,调整大小需要创建一个新实例)。如果他返回,比如说,
new List()
,它会改变情况吗?因为从该方法返回时它将被转换为
IEnumerable
是一个更昂贵的操作,因为它将创建列表的实例(并在过程中为其分配内存)
public IEnumerable<Friend> FindFriends()
{
    return userExists ? 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
        }): new List<Friend>();
}