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

  • Home
  • SEARCH
  • 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 7077383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:25:00+00:00 2026-05-28T06:25:00+00:00

How can i cast a List<object> to List<SomethingElse> ? ( where SomethingElse is known

  • 0

How can i cast a List<object> to List<SomethingElse>?

(where SomethingElse is known to descend from object)


Bonus Chatter

Casting the list:

List<Object> first = ...;

List<SomethingElse> second = (List<SomethingElse>)first;

doesn’t work:

Cannot convert type ‘System.Collections.Generic.List’ to ‘System.Collections.Generic.List’

Casting the list:

List<SomethingElse> second = first.Cast<SomethingElse>();

doesn’t work:

Cannot implicitely convert type ‘System.Collections.Generic.List’ to ‘System.Collections.Generic.List’

i don’t actually need the full List<T> object, just an ICollection<T> will do:

ICollection<SomethingElse> second = first;
ICollection<SomethingElse> second = (ICollection<SomethingElse>)first;
ICollection<SomethingElse> second = first.Cast<SomethingElse>();

don’t work.

  • 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-28T06:25:00+00:00Added an answer on May 28, 2026 at 6:25 am

    LINQ, as implemented through the extension methods within the Enumerable class, relies on deferred execution:

    Methods that are used in a query that returns a sequence of values do not consume the target data until the query object is enumerated. This is known as deferred execution.

    Cast<T> does not create a new list immediately, but rather stores all the information that is required to perform the action. The list would only get enumerated when required (for example, through a foreach statement).

    In your case, if you simply intend to iterate over the sequence, you should consider sticking to the IEnumerable<T> interface, which is the declared return type of Cast<T>:

    IEnumerable<SomethingElse> second = first.Cast<SomethingElse>();
    foreach (SomethingElse se in second)
    {
        // ...
    }
    

    This is efficient, since it only performs the cast as each item is iterated.

    If you’re convinced you want a new list to be created immediately, use ToList:

    List<SomethingElse> second = first.Cast<SomethingElse>().ToList();
    

    Edit: Replying to point posted in comment:

    It depends on what you mean by “a list that can be modified”. There are several LINQ query operators that will allow you to alter the definition of your query further. For example, if you want to remove all SomethingElse elements whose IsDeleted property is true, you can use the Where operator:

    IEnumerable<SomethingElse> second = first.Cast<SomethingElse>();
    second = second.Where(element => !element.IsDeleted);
    

    If you want to add a sequence of new elements, you can use the Concat operator:

    second = second.Concat(anotherCollectionOfSomethingElse);
    

    If you want to sort your sequence in ascending order of ID, use the OrderBy operator:

    second = second.OrderBy(element => element.ID);
    

    Each time, we’re applying a query operator over the former definition of our query, and assigning the new (composite) query to our second variable. LINQ would store all your operators in the query definition. Then, when the sequence is actually enumerated (for example, through a foreach or ToList), it would give you the composite result of your sequence, with all the query operators applied in order.

    As with all cases of deferred execution / lazy evaluation, be careful not to go overboard with this. If, for example, you’re going to apply a Where operator which will reduce the size of your sequence drastically, it might make sense to execute the query eagerly and store the enumerated list instead.

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

Sidebar

Related Questions

Everything inherits from object. It's the basis of inheritance. Everything can be implicitly cast
Why can you not cast a object which at runtime has type object {System.Collections.Generic.List<string[]>}
Can we cast List<SomeClass> in to List<object> int .net.
From my question at here: Cast in List object I accepted the answer using
public void Foo (IEnumerable<object> objects) { } var strings = new List<string>{first, second, third};
Can you cast a List<int> to List<string> somehow? I know I could loop through
I have a QVariant object within a QTreeWidgetItem, how can I cast it to
I have a class Something that implements ISomething . How can I convert/cast from
I'm trying to do cast a List to an IEnumerable, so I can verify
I have a list of object properties read from xml file and want to

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.