Skip to main content
added 1827 characters in body
Source Link
Arseni Mourzenko
  • 139.1k
  • 32
  • 359
  • 544

Two reasons.

An array, in C#, is a weird data structure: its length is immutable, but its elements are mutable.An array, in C#, is a weird and very special data structure: its length is immutable, but its elements are mutable. Such specificity is rarely needed. Actually, in twenty years of software development, I haven't yet seen a single case in C# where the author of a piece of code was actively seeking this specificity. I've seen a lot of cases where arrays were used wrongly, by someone who didn't know what arrays are, nor what are the other types, such as hash sets, collections, lists, queues, or double linked lists.

Also, an array is a bit low level for general usean array is a bit low level for general use—it could make sense in C++, but less in C#. A listList<T> is based on an array, and provides a good abstraction level over the actual way elements are stored in an array, or what happens when a list grows beyond the initial capacity of the underlying array. You may intentionally seek the low level abstraction provided by an array, but that shouldn't be your default choice by any means. And if you actually want to do seek low level stuff, spansSpan<T> may possibly be what you are looking for.

For example, I might write a method that does "search the product index and return matching products". Or "get me customer's orders between two dates".

You won't need arrays here, or any type of collection.

These kinds of access patterns do not imply any sort of modification need [...] the size of these arrays never changes.

If nothing changes, an array is a bad data type, as it implies that the elements are mutable.

In the case of an API that does not imply mutability, why should I want to return a type like List that implies the list is mutable?

You shouldn't. But neither should you return an array, for the exact same reason you shouldn't use lists here.

the array notation is built-in to the language

If you want to go the route of notations and built-ins, check collection expression syntax. Hint:

ICollection<int> d = [1, 2, 3];

has absolutely no arrays involved. It does involve lists, spans, and collections, however.

and syntactically simpler

What about the actual usage?

Junior programmer creates an array x.

Junior programmer wants to add an element to x.

He types x.Add(42), and is immediately shouted at by a compiler.

He tries to understand the message, but it's not an easy task. All examples he has seen use Add on collections. So why those examples work, whereas in his code, Add doesn't exist?

LINQ adds most of the functionality for projection, searching, sorting, etc that array does not contain by default

Nor does a list.

Two reasons.

An array, in C#, is a weird data structure: its length is immutable, but its elements are mutable. Such specificity is rarely needed. Actually, in twenty years of software development, I haven't yet seen a single case in C# where the author of a piece of code was actively seeking this specificity. I've seen a lot of cases where arrays were used wrongly, by someone who didn't know what arrays are, nor what are the other types, such as hash sets, collections, lists, queues, or double linked lists.

Also, an array is a bit low level for general use. A list is based on an array, and provides a good abstraction level over the actual way elements are stored in an array, or what happens when a list grows beyond the initial capacity of the underlying array. You may intentionally seek the low level abstraction provided by an array, but that shouldn't be your default choice by any means. And if you do seek low level stuff, spans may possibly be what you are looking for.

Two reasons.

An array, in C#, is a weird and very special data structure: its length is immutable, but its elements are mutable. Such specificity is rarely needed. Actually, in twenty years of software development, I haven't yet seen a single case in C# where the author of a piece of code was actively seeking this specificity. I've seen a lot of cases where arrays were used wrongly, by someone who didn't know what arrays are, nor what are the other types, such as hash sets, collections, lists, queues, or double linked lists.

Also, an array is a bit low level for general use—it could make sense in C++, but less in C#. A List<T> is based on an array, and provides a good abstraction level over the actual way elements are stored in an array, or what happens when a list grows beyond the initial capacity of the underlying array. You may intentionally seek the low level abstraction provided by an array, but that shouldn't be your default choice by any means. And if you actually want to do low level stuff, Span<T> may possibly be what you are looking for.

For example, I might write a method that does "search the product index and return matching products". Or "get me customer's orders between two dates".

You won't need arrays here, or any type of collection.

These kinds of access patterns do not imply any sort of modification need [...] the size of these arrays never changes.

If nothing changes, an array is a bad data type, as it implies that the elements are mutable.

In the case of an API that does not imply mutability, why should I want to return a type like List that implies the list is mutable?

You shouldn't. But neither should you return an array, for the exact same reason you shouldn't use lists here.

the array notation is built-in to the language

If you want to go the route of notations and built-ins, check collection expression syntax. Hint:

ICollection<int> d = [1, 2, 3];

has absolutely no arrays involved. It does involve lists, spans, and collections, however.

and syntactically simpler

What about the actual usage?

Junior programmer creates an array x.

Junior programmer wants to add an element to x.

He types x.Add(42), and is immediately shouted at by a compiler.

He tries to understand the message, but it's not an easy task. All examples he has seen use Add on collections. So why those examples work, whereas in his code, Add doesn't exist?

LINQ adds most of the functionality for projection, searching, sorting, etc that array does not contain by default

Nor does a list.

deleted 1 character in body
Source Link
Robert Harvey
  • 200.7k
  • 55
  • 470
  • 683

Two reasons.

An array, in C#, is a weird data structure: its length is immutable, but its elements are mutable. Such specificity is rarely needed. Actually, in twenty years of software development, I haven't yet seen a single case in C# where the author of a piece of code was actively seeking this specificity. I've seen a lot of cases where arrays were used wrongly, by someone who didn't know what arrays are, nor what are the other types, such as hash sets, collections, lists, queues, or double linked lists.

Also, an array is a bit low level for general use. A list is based on an array, and provides a good abstraction level over the actual way elements are stored in an array, or what happens when a list grows beneathbeyond the initial capacity of the underlying array. You may intentionally seek the low level abstraction provided by an array. But, but that shouldn't be your default choice by noany means. And if you do seek low level stuff, spans may possibly be what you are looking for.

Two reasons.

An array, in C#, is a weird data structure: its length is immutable, but its elements are mutable. Such specificity is rarely needed. Actually, in twenty years of software development, I haven't yet seen a single case in C# where the author of a piece of code was actively seeking this specificity. I've seen a lot of cases where arrays were used wrongly, by someone who didn't know what arrays are, nor what are the other types, such as hash sets, collections, lists, queues, or double linked lists.

Also, an array is a bit low level for general use. A list is based on an array, and provides a good abstraction level over the actual way elements are stored in an array, or what happens when a list grows beneath the initial capacity of the underlying array. You may intentionally seek the low level provided by an array. But that shouldn't be your default choice by no means. And if you do seek low level stuff, spans may possibly be what you are looking for.

Two reasons.

An array, in C#, is a weird data structure: its length is immutable, but its elements are mutable. Such specificity is rarely needed. Actually, in twenty years of software development, I haven't yet seen a single case in C# where the author of a piece of code was actively seeking this specificity. I've seen a lot of cases where arrays were used wrongly, by someone who didn't know what arrays are, nor what are the other types, such as hash sets, collections, lists, queues, or double linked lists.

Also, an array is a bit low level for general use. A list is based on an array, and provides a good abstraction level over the actual way elements are stored in an array, or what happens when a list grows beyond the initial capacity of the underlying array. You may intentionally seek the low level abstraction provided by an array, but that shouldn't be your default choice by any means. And if you do seek low level stuff, spans may possibly be what you are looking for.

Source Link
Arseni Mourzenko
  • 139.1k
  • 32
  • 359
  • 544

Two reasons.

An array, in C#, is a weird data structure: its length is immutable, but its elements are mutable. Such specificity is rarely needed. Actually, in twenty years of software development, I haven't yet seen a single case in C# where the author of a piece of code was actively seeking this specificity. I've seen a lot of cases where arrays were used wrongly, by someone who didn't know what arrays are, nor what are the other types, such as hash sets, collections, lists, queues, or double linked lists.

Also, an array is a bit low level for general use. A list is based on an array, and provides a good abstraction level over the actual way elements are stored in an array, or what happens when a list grows beneath the initial capacity of the underlying array. You may intentionally seek the low level provided by an array. But that shouldn't be your default choice by no means. And if you do seek low level stuff, spans may possibly be what you are looking for.