Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3786672
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:43:12+00:00 2026-05-19T11:43:12+00:00

I have in instance of class foo and i want to return it as

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T11:43:13+00:00Added an answer on May 19, 2026 at 11:43 am

    Options:

    • Create an instance of a collection class, like an array or a list. This would be mutable by default, which would be slightly unhelpful if this is a sequence you want to be able to hand out in your API. You could create a ReadOnlyCollection<T> wrapper around such a collection though.
    • Write your own iterator block as per Botz3000’s answer
    • Use 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 Concat on some other sequence, you might want:

    var wholeList = regularList.Concat(new[] { finalValue });
    

    I have confidence that Concat isn’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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code similar to this: class Foo { List<Bar> _myList; ... public IEnumerable<Bar>
If I have for example a class with instance method and variables class Foo
Let's say we have a class foo which has a private instance variable bar
Let's say I have a class Foo with some primitive instance variables. I initialize
I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
I have an instance of Class A that I want to refer to in
I have a class whose new method has been made private because I want
Assume I have an instance foo of some class Class with some variable foo.x.
Assuming I have a instance of class A (name it AI) that has an
In my application (written in C#) I have an instance of a class with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.