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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:37:12+00:00 2026-06-02T03:37:12+00:00

Possible Duplicate: Collection<T> versus List<T> what should you use on your interfaces? Consider this

  • 0

Possible Duplicate:
Collection<T> versus List<T> what should you use on your interfaces?

Consider this method—the returned variable myVar is a List<T>, but the return type of the method MyMethod() is an IEnumerable<T>:

public IEnumerable<T> MyMethod(string stuff)
{
     var myVar = new List<T>();
     //do stuff

     return myVar;
}

Essentially, what I want to know is if returning myVar as a different type is OK.

Specifically, with respect to my situation, the ‘do stuff’ goes through a DataRow and assigns the items in that DataRow to a list of objects. I also have similar situations with ICollection and IList return types in other places.

The reason I want to return either IEnumerable or ICollection is so that I’m not returning more than needed. But at the same time, this allows the caller to convert the returned value to a List if it needs to do so.

However, it seems weird that my return statement is returning a List, instead of what the method’s return type is. Is this normal practice? Is there anything wrong with doing this?

Clarification (in response to the dupe comments):

Just to clarify, what I’m curious about is if it is okay that my return statement in the body is returning a List<T>, but the method header has a return-type of IEnumerable, or possibly ICollection, Collection, etc… Something different than than bodies return statement.

  • 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-06-02T03:37:16+00:00Added an answer on June 2, 2026 at 3:37 am

    Not only there’s nothing wrong with it, but it’s actually good practice: expose only what is strictly necessary. That way, the caller can’t rely on the fact that the method will return a List<T>, so if for some reason you need to change the implementation to return something else, you won’t break your contract. However the calling code might break if it (incorrectly) made assumptions about what the method actually returns.

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

Sidebar

Related Questions

Possible Duplicate: C#: Difference between List<T> and Collection<T> (CA1002, Do not expose generic lists)
Possible Duplicate: How to implement garbage collection in C++ I was recently asked this
Possible Duplicate: The most sophisticated way for creating comma-separated Strings from a Collection/Array/List? I
Possible Duplicate: Collection initialization syntax in Visual Basic 2008? This does not compile. Dim
Possible Duplicate: Getting exception as “Collection was mutated while being enumerated” This question is
Possible Duplicate: Use of var keyword in C# Which declaration should be accepted ?
Possible Duplicate: When to use LinkedList<> over ArrayList<>? This is a genuine attempt to
Possible Duplicate: Collection initialization syntax in Visual Basic 2008? How is the following C#
Possible Duplicate: Which collection for storing unique strings? I am currently using a Dictionary<string,
Possible Duplicate: Finding the Variable Name passed to a Function in C# In C#,

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.