Often I can see a statement like this: "IEnumerable: Represents a collection of objects ....".
- What is a collection in terms of the selected programming language, for example, CSharp?
- Do you think that IEnumerable is a collection?
Often I can see a statement like this: "IEnumerable: Represents a collection of objects ....".
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Mariusz PostolPosted Aug 23, 2024, 9:10 AM
"In C#, a collection is a class that holds and manages multiple objects" - arrays for example comply with this definition but are collections? We cannot define the collection term using the collection word!!?
"...holds and manages multiple objects." - a class cannot manage any object because it is compile-time text but the objects are instantiated at run time.
Sorry, but it doesn't clear things up!
Gowtham CpPosted Aug 23, 2024, 8:41 AM
Hi Mariusz,
Got it.
In C#, a collection is a class that holds and manages multiple objects. Examples include
List,Dictionary, andQueue. These classes handle storage and offer methods to work with the data they contain.On the other hand,
IEnumerableis an interface that provides a way to iterate over a sequence of elements. It doesn’t manage or store data itself; it’s used by collections to enable looping through items.So, while
IEnumerablehelps with iterating over collections, it’s not a collection itself but a tool for working with collections.Hope that clears things up!
Best,
Gowtham
Mariusz PostolPosted Aug 23, 2024, 7:42 AM
@Gowtham Cp - I am looking for an answer that allows us to avoid discussion on the meaning of the "way" word in terms of the language. for example. Let me stress in terms of a programming language but not in terms of natural language. It is like mathematics, but not sociology, for example, because CS and IT were derived from mathematics. There must be no doubts.
You said "IEnumerable isn’t exactly a collection" but I know authors stating something different.
Sorry, but I cannot mark it as an answer. Again, thanks for your contribution.
Gowtham CpPosted Aug 22, 2024, 3:49 PM
1. In C#, a collection is a way to store and manage a group of objects, like a list or an array. It’s a core concept in the C#, helping you organize and work with multiple items.
2. IEnumerable isn’t exactly a collection, it’s more of an interface that allows you to loop through a collection. It lets you access items one by one but doesn’t hold any data itself. So, while
IEnumerablehelps you navigate collections, it’s not a collection in itself.Hope it helps!