Skip to main content

Timeline for C#'s Aversion to Array

Current License: CC BY-SA 4.0

10 events
when toggle format what by license comment
Jun 24 at 19:55 comment added Filip Milovanović Yeah, as I mentioned, under the hood things aren't so clear cut because the compiler does all kinds of shenanigans - e.g., if I'm not mistaken, for LINQ and the foreach loop, it doesn't actually require specific types or interfaces, but looks for the presence of certain methods, a form of duck-typing. The Add method in the quote is needed because here a coll. expression is used to create & initialize a collection with a predefined set of values, so the Add method is used to add them - but from the wording ("usually"), the compiler is probably treating certain types/scenarios in a special way.
Jun 24 at 13:12 comment added Olivier Jacot-Descombes It depends on the context. E.g. the article Collection expressions - C# language reference says: "A collection expression can be converted to different collection types, including:", then a list of different span types and arrays follows and the text continues with "[...] supports IEnumerable<T> and there's an accessible Add method to add items to the collection."
Jun 23 at 23:54 comment added Filip Milovanović ICollection is a bit of a legacy interface that predates LNIQ, and is not particularly useful, but it does represent a mutable collection. Yeah, IEnumerable<T> doesn't describe all aspects of all collections, but a fundamental aspect of any collection (a countable set of things) is that you can enumerate it (regardless of how the elements are obtained or produced). Since C# 2.0, a class is considered a collection if it implements IEnumerable or IEnumerable<T> - this is how Microsoft docs and C# language designers talk about it (though behind the scenes it's a bit more complicated).
Jun 23 at 12:32 history edited Olivier Jacot-Descombes CC BY-SA 4.0
added 53 characters in body
Jun 23 at 12:30 comment added Olivier Jacot-Descombes You are right, but IEnumerable<T> does not describe the collection aspect (ICollection<T> and IList<T> do that) but only the enumeration aspect.
Jun 22 at 21:46 comment added Filip Milovanović Your final paragraph is not exactly wrong, but isn't quite right either. IEnumerable<T> is an interface that (pretty much) every collection in C# implements, List<T> being a prime example. In the OO lingo, a List<T> "is-an" IEnumerable<T>, which is why lists work with LINQ. Same goes for arrays. Most common implementations of IEnumerable<T> are collections and do in fact store data, but some implementations can generate elements as you iterate through them - yield return being syntactic sugar that makes writing these sorts of enumerables (and their associated enumerators) easier.
Jun 22 at 13:35 history edited Olivier Jacot-Descombes CC BY-SA 4.0
deleted 3 characters in body
Jun 21 at 13:31 history edited Olivier Jacot-Descombes CC BY-SA 4.0
added 466 characters in body
Jun 20 at 16:52 history edited Olivier Jacot-Descombes CC BY-SA 4.0
added 2 characters in body
Jun 20 at 16:47 history answered Olivier Jacot-Descombes CC BY-SA 4.0