Timeline for C#'s Aversion to Array
Current License: CC BY-SA 4.0
21 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jul 12 at 11:29 | audit | First answers | |||
| Jul 12 at 11:30 | |||||
| Jun 26 at 15:45 | comment | added | Caleth | @ojs We aren't saying that it's a complex type, just one that doesn't have compelling use cases, when contrasted with other container types. | |
| Jun 26 at 15:42 | comment | added | Caleth |
@ojs std::vector<T>(count) does what your desired array type would. Unless you really need to be able to mutate the contents and at the same time never change the count, which is a situation Arseni hasn't encountered, and nor have I.
|
|
| Jun 26 at 15:38 | comment | added | ojs | @Caleth the answer does not explain why array with fixed length and mutable contents is weird, it just states it as if it was a fact. And yes, that is wonderfully complicated code and the standard library has far less useful things than a refactored version of that would be. | |
| Jun 26 at 8:22 | comment | added | Caleth |
@ojs This answer explains how it is weird. A unique_ptr<T[]> tied to a span<T> is a perfectly cromulent contiguous_range of T: godbolt.org/z/6fonhGc9Y
|
|
| Jun 25 at 18:51 | comment | added | ojs | @Caleth it's not compatible with STL-style algorithms and that makes it mostly useless. And just because you think something is weird doesn't mean it is. | |
| Jun 25 at 17:54 | comment | added | Caleth |
@ojs there's std::make_unique<T[]>, but again, it's a wierd requirement
|
|
| Jun 25 at 14:51 | comment | added | ojs | @Caleth After getting used to C#, the lack of runtime-determined fixed size container in C++ feels just odd and wrong | |
| Jun 25 at 7:22 | comment | added | Caleth | @nvoigt its wierd in every language where it is like that, but the question is asking about c# | |
| Jun 24 at 14:35 | comment | added | Brian |
To extend Arseni's point about IEnumerable<T> vs read-only collections, see discussion here. If you need to enumerate a collection more than once, a readonly collection is often preferable over IEnumerable<T>..
|
|
| Jun 23 at 14:19 | comment | added | nvoigt | "An array, in C#, is a weird and very special data structure: its length is immutable, but its elements are mutable." Isn't that the definition of an array in almost any language? Why is it weird? | |
| Jun 23 at 6:18 | comment | added | Thorbjørn Ravn Andersen | @K0D4 You should not switch but instead use the best type for the job at hand. | |
| Jun 22 at 21:58 | comment | added | Arseni Mourzenko |
@puck: if nothing changes, than IEnumerable<T> should be used if the task consists of walking through the data. If, for some reason, the data needs to be stored in memory, then one can use one of the read-only collection types to make the immutability explicit. Also, as it may not be very clear, immutability here refers to the fact of being able to set the elements of a collection. It says nothing about the mutable character of the elements themselves. In other words, an immutable collection may store references to mutable objects.
|
|
| Jun 22 at 16:58 | comment | added | puck | "If nothing changes, an array is a bad data type, as it implies that the elements are mutable." - this somehow implies that a list or collection only can hold immutable elements? | |
| Jun 21 at 10:28 | comment | added | ojs | How do you use Span<T> without an underlying array somewhere? | |
| Jun 20 at 20:28 | comment | added | Arseni Mourzenko | “This is not enough to convince me to switch everything from using arrays”: well, what about editing your question by providing an example of a piece of code you've seen, where an array is an appropriate data structure? | |
| Jun 20 at 20:25 | comment | added | Arseni Mourzenko |
I would imagine that when you see byte[] in code, it's usually low-level code itself, and is usually written by developers with C background. The last time I used byte[] was when I was manipulating bitmaps to do some image analysis from cameras—if I had to do it today, I would be using Span<T>, or simply switch to C++. The last time I've seen byte[] was in cryptography code in .NET—not something an average enterprise programmer would write on daily basis. No, wait: this morning, I've spotted byte[] in business code. But it was wrong. They shouldn't have used collections there.
|
|
| Jun 20 at 20:08 | comment | added | K0D4 |
This is not enough to convince me to switch everything from using arrays, but your comment about low-levelness of arrays has me thinking. If we regard arrays as too low-level, then is the choice of using an array over a collection type based on what we're sticking into it? A byte[] is a common case where we see arrays used in C#, perhaps the serial nature of the data lends itself to thinking in array terms, where as a MyClass[] being more of a serial list of pointers to objects implies a different use pattern?
|
|
| Jun 20 at 19:36 | history | edited | Arseni Mourzenko | CC BY-SA 4.0 |
added 1827 characters in body
|
| Jun 20 at 19:22 | history | edited | Robert Harvey | CC BY-SA 4.0 |
deleted 1 character in body
|
| Jun 20 at 19:08 | history | answered | Arseni Mourzenko | CC BY-SA 4.0 |