2

In C#, I was told that if a container class such as List(T) is first upcast to a container interface such as IEnumerable, and then subsequently iterated over using foreach then runtime garbage will be created. Also, even when fully downcast, I was told that iterating over Collection(T) also creates references on the heap. I understand that this is a result of a virtual call to GetEnumerator() which may return a reference or value type result.

Inspection of the MSDN docs for values types clearly lists all enumerations as value type. If an enumeration consists of an enumerator list then aren't these enumerators of value type as per the docs? are they boxed? or completely unrelated to each other but with similar names? or something else entirely?

I'm not sure how to unify these two statements and I was hoping someone could explain it to me more plainly.

Thanks.

EDIT: Question rephrased taking into account commentor suggestions on use of words such as 'never' and 'un-necessary'

1 Answer 1

2

Enumarations (enums) are unrelated to Enumerators.

And

I was told that to avoid...

Seems like a very premature and unnecessary optimization.

answered Apr 17, 2012 at 10:13
Sign up to request clarification or add additional context in comments.

3 Comments

If that foreach is performing constant runtime allocation and my heap is complex, I cannot allow a collection to occur. Under the .NET compact framework with XNA/Xbox 360 a stop-the-world garbage collection occurs every time a megabyte is allocated.
You didn't mention or tag XNA... The default assumption is the normal Fx.
Indeed, but my question was not about optimisation. Just telling you why I'm worried about it. This behaviour is not specific to XNA, I saw no need to tag it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.