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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:48:57+00:00 2026-06-15T13:48:57+00:00

My class implements IEnumerable<T> . Class also implements: void AddRange(IEnumerable<T> items) { // items

  • 0

My class implements IEnumerable<T>. Class also implements:

void AddRange(IEnumerable<T> items) 
{
    // items can be a deffered query against the collection items being added to
    var itemslist = items.ToList();
    ...processing and adding...
}

But I have some doubts whether convertion to a static collection is a good design.

For example List itself has nothing like this:

var list = new List<int> {1, 2, 3};
list.AddRange(list.Select(i => i+list.Count).ToList());

No exception.

var list = new List<int> {1, 2, 3};
list.AddRange(list.Select(i => i+list.Count));

InvalidOperationException : collection was modified during enumeration.

  • 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-15T13:48:58+00:00Added an answer on June 15, 2026 at 1:48 pm

    List<T>.AddRange will behave differently depending on whether the sequence it’s passed implements ICollection<T>. In the first case, List<T> implements ICollection<T>, so AddRange can call CopyTo to do a bulk copy. In the second you’re just passing in an arbitrary IEnumerable<T>, so it has to iterate over it in order to proceed.

    Fundamentally you’re doing something odd in the second case though. The simple answer is not to do that. I wouldn’t expect the results to be guaranteed – it should be up to the implementation whether it eagerly iterates over the passed in sequence and then copies it, or whether it does it lazily. Either implementation will work fine for any sane sequence.

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

Sidebar

Related Questions

I have a generic class, Class<T> , that implements IEnumerable<T> . T is also
I have 2 instances of a class that implements the IEnumerable interface. I would
Is this a bad idea? Private Class GH_DataStructureEnumerator(Of Q As Types.IGH_Goo) Implements IEnumerable(Of Q)
From code how can we know which interfaces one class implements? Example: interface IDrink
class Parent implements Serializable{ ........ } class Child extends Parent{ private void writeObject(ObjectOutputStream oos)
class Name implements Runnable { public void run() { for (int x = 1;
I have class X that implements an interface IX . I also have a
I'm having problems implementing IEnumerable<T> in my custom collection class in C++/CLI. Here is
I have a class that implements IEnumerable , but doesn't implement IEnumerable<T> . I
Dictionary<TKey, TValue>.KeyCollection Class implements the contains through extension method from IEnumerable , which is

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.