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 928841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:04:32+00:00 2026-05-15T20:04:32+00:00

I have often the case where I want to return an Enumerable<T> from a

  • 0

I have often the case where I want to return an Enumerable<T> from a method or a property. To build the returning Enumerable<T>, I use a List<T>-instance. After filling the list, I return the list.

I always thought that this is enough. But it exists the possibility that the caller casts the resulting Enumerable<T> back into the List<T> and begins to work further with it. If in a later time I change the implementation of my method, the caller’s code will fail. To avoid this, I could return list.ToArray or make a read-only list before returning it to the caller. But for me this seems to be a big overkill. What do you think?

Please note, I never will return an internally used list so that the caller can change my objects internal state. The question is only about a short living list that is built temporary to hold the return values.

IEnumerable<string> GetAList() {
    List<string> aList = new List<string>();
    aList.Add("a");
    aList.Add("b");
    return aList;
}

IEnumerable<string> GetAList() {
    List<string> aList = new List<string>();
    aList.Add("a");
    aList.Add("b");
    return aList.ToArray<string>();
}

The examples are super-simple and in this case I would work from the beginning on with arrays, but it’s only to show explain the question.

  • 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-15T20:04:33+00:00Added an answer on May 15, 2026 at 8:04 pm

    No, this is fine.

    This is an example of ‘polymorphism’ at work. Because the caller to the method is only interested in an IEnumerable<string>, the internal workings of the method are free to return whatever class it likes as long as it derives from the IEnumerable<string> interface.

    If the caller takes the IEnumerable<string> and casts up to List<string> then they have broken the contract, which only states that an 'IEnumerable<string> will be returned.

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

Sidebar

Related Questions

Often, I will have an array holding a set of values each of which
Often while coding view templates in html, my habit of adding some helpful comments
Firstly, I want to restrict this question to web development only. So this is
There is a constant change (!) in our database, new columns are often added.
I'm getting the Save unsaved transient entities error in NHibernate. I have an aggregate
After going through a lot of articles on IDisposable , I got confused about
Even after years of programming, I'm ashamed to say that I've never really fully
I come from a computer science. background, but I am now doing genomics. My
I am trying to write a class template that provides a comparison operator between
I built a form in sales force and added it to my site. The

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.