How can I add an item to a IEnumerable collecti

2019-01-02 19:18发布

My question as title above. For example,

IEnumerable<T> items = new T[]{new T("msg")};
items.ToList().Add(new T("msg2"));

but after all it only has 1 item inside.

Can we have a method like items.Add(item)?

like the List<T>

13条回答
呛了眼睛熬了心
2楼-- · 2019-01-02 20:17

Sure, you can (I am leaving your T-business aside):

public IEnumerable<string> tryAdd(IEnumerable<string> items)
{
    List<string> list = items.ToList();
    string obj = "";
    list.Add(obj);

    return list.Select(i => i);
}
查看更多
登录 后发表回答