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:
IEnumerable<foo>.fromInstance(foo)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Options:
ReadOnlyCollection<T>wrapper around such a collection though.Enumerable.Repeat(item, 1)from LINQ, if you’re using .NET 3.5.The best answer here depends on the usage. If you only need this to call another method which uses a sequence, and you know it won’t be modified, I’d probably use an array. For example, in order to call
Concaton some other sequence, you might want:I have confidence that
Concatisn’t going to mutate the array, and nothing else will ever see the reference to the array itself.If you need to return the sequence to some other code, and you don’t know what it might do with it, I’d probably use
Enumerable.Repeat.