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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:16:34+00:00 2026-05-21T16:16:34+00:00

I’m trying to create a small proof-of-concept application for my boss, however the code

  • 0

I’m trying to create a small proof-of-concept application for my boss, however the code I’ve created that simulates what he’s trying to pull off isn’t working.

for (int i = 0; i < 5000; i++)
{
     ((IList<string>) obj2.Stuff).Add("Iteration " + i.ToString());
}

I’m trying to pull this off all in one line because this is what his code looked like the other day in the framework we’re working on. Anywho when the code above executes, I get a runtime error saying “Collection was of a fixed-size”. And when I try casting to a List instead of an IList, I get an InvalidCastException saying “Unable to cast object of type ‘System.String[]’ to type ‘System.Collections.Generic.List`1[System.String]’.”

Anybody have any ideas on how I can pull off a single-line cast to add an item to the IEnumerable or help me figure out a way around the two errors I keep getting? Thanks in advance.

EDIT (4/19/2011 10:49AM EST)
I’m adding more code to help people out — probably should’ve done this earlier. Sorry.

Program.cs:

        #region Cast Test
        Class1 obj2 = new Class1();
        obj2.Stuff = Enumerable.Empty<string>();

        Console.WriteLine("Cast - Start Time: " + 0 + "ms");

        Stopwatch stopwatch2 = new Stopwatch();
        stopwatch.Start();

        for (int i = 0; i < 5000; i++)
        {
            ((IList<string>) obj2.Stuff).Add("Iteration " + i.ToString());
        }

        stopwatch2.Stop();
        Console.WriteLine("Cast - Stop Time: " + stopwatch2.ElapsedMilliseconds.ToString() + "ms");
        #endregion

Class1.cs:

public class Class1
{
    private IEnumerable<string> stuff;

    public IEnumerable<string> Stuff
    {
        get { return stuff; }
        set { stuff = value; }
    }
}
  • 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-21T16:16:35+00:00Added an answer on May 21, 2026 at 4:16 pm

    In comments on one of the answers above you say “I’m trying to avoid copying the list.”

    You are trying to add data to an object whose underlying type is IEnumerable<T>. But an IEnumerable object is not an actual container, it’s an interface. You can’t add stuff to an interface. But you can assign another object that implements that interface to it.

    So to use foson’s example above, you could just do:

    obj2.Stuff = Enumerable.Range(0, 5000).Select(i => "Iteration " + i.ToString());
    

    Note that when this code executes, nothing actually happens. No objects will be created until something actually iterates over obj2.Stuff. When that happens, the methods in LINQ will be called that create objects one at a time and return them to the iterator loop.

    But there’s no actual storage device involved here. You can iterate over obj2.Stuff and unless you consequently added each integer to something else, they would be gone at the next iteration.

    The fundamental point here is you can’t store things in IEnumerable, rather, IEnumerable is a way to return objects in sequence, and that could be from a list construct, or from a function that generates a sequence.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is 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.