return single instance object as IEnumerable

后端 未结 4 1739
夕颜
夕颜 2021-01-01 17:10

I have in instance of class foo and i want to return it as IEnumerable. Can i do it without creating a new list etc..

Perhaps something like the following:



        
4条回答
  •  星月不相逢
    2021-01-01 17:22

    IENumerable is supposed to be used for something that you can enumerate through, so using it for a single instance seems quite strange. If you really need to, you can get it done like this. It might be a better way, but this should get the job done:

    List myList = new List();
    myList.Add( myInstanceOfFoo );
    IEnumerable myEnumerable = myList.AsEnumerable();
    

    Regardless of how you see this, you are actually trying to make a list of one element, and then it's really no reason to make it a list.

提交回复
热议问题