.NET 中的数组已经失去意义了吗?

发布于 2024-09-18 22:28:02 字数 58 浏览 5 评论 0原文

对于每一种需要使用数组的情况……都有一个很棒的集合,它有很多好处。 .NET 中还有数组的具体用例吗?

For every situation that warrants the use of an array ... there is an awesome collection with benefits. Is there any specific use case for Arrays any more in .NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

南街九尾狐 2024-09-25 22:28:02

我想到了发送/接收特定长度的数据,即。串行端口、Web 请求、FTP 请求。基本上是在系统较低级别上工作的东西。此外,大多数集合都使用数组进行存储(值得注意的例外:LinkedList)。集合只是另一个抽象层。

Sending/Receiving data with a specific length comes to mind, ie. Serial Port, Web Request, FTP Request. Basically stuff that works on a lower level in the system. Also, most Collections are using an array for storage (Noteable exception: LinkedList<T>). Collections are just another abstraction layer.

傲世九天 2024-09-25 22:28:02

数组很有用,因为它们在内存中始终是线性的,并且使用起来速度很快。例如,我可以将 byte[] 直接编组到结构中,没有任何问题,但 List 必须首先转换为数组我知道。

Arrays are useful because they are always linear in memory and are fast to work with. For example I can take a byte[] and marshal directly into a structure without any problems but a List<T> would have to be converted to an array first as far as I know.

别闹i 2024-09-25 22:28:02

不,它们仍然有其用途,应该始终予以考虑。

请记住,数组是固定长度的非常基本的表示形式,因此它们非常快,并且大多数语言根据数组中使用的类型来理解它们。

您需要在创建数组时定义数组大小,并且以后无法更改其大小。列表和其他内容可以根据需要增长,这会增加内存分配的开销。

列表和其他类型很有用,因为它们可以做很多事情,但有时您不需要所有额外的开销,因此数组就足够了。

这就像驾驶一辆四驱车,因为您认为有一天您可能需要离开公路,尽管您有 99.9% 的机会会在正常道路上行驶。数组将是基本的汽车,例如列表将是 4x4...它可以完成汽车可以做的所有其他事情(引擎盖下可能使用大部分相同的部件),但以汽油为代价,成本可能不会适合某些停车位等...

数组=性能和兼容性

列表(或其他表示)=以性能和兼容性为代价的易用性

No they still have their uses and should always be considered.

Remember arrays are very basic representations of a fixed length so they are very fast and most languages understand them depending on the type used in the array.

You need to define an array size at the time that it is created and cannot change its size later. Lists and other things can grow as needed which adds overhead with respect to memory allocation.

Lists and other types are useful because they can do a lot but sometimes you don't need all that extra overhead so an array is all you need.

It's like driving a 4x4 because you think one day you might need to go off roading even though there is 99.9% of a chance you will be on normal roads. Array would be the basic car and a List for example would be a 4x4... it does everything else a car can do (an under the hood might use most of the same parts) but at the expense of gas, cost, might not fit in certain parking stalls, etc...

Arrays = performance and compatability

Lists (or other representations) = ease of use at a cost of performance and compatability

人间☆小暴躁 2024-09-25 22:28:02

是的,数组当然仍然有用。有些方法仍然需要数组。

例如:

string[] items = "a,b;c:d".Split(new char[]{',',';',':'});

它仍然是保留一堆项目的最简单方法,并且是第一选择,直到您需要某些特定功能(例如动态增长)。

Yes, there certainly still is a use for arrays. Some methods still needs arrays.

For example:

string[] items = "a,b;c:d".Split(new char[]{',',';',':'});

It's still the simplest way to keep a bunch of items, and the number one choice until you need some specific feature, like for example dynamic growth.

天邊彩虹 2024-09-25 22:28:02

数组是否失去了(某些)意义?

是的。对于许多需要项目“表”的任务,现在有更灵活和有用的解决方案,例如 ListIEnumerable

数组已经不再重要了吗?

不。它们是最快的存储形式,并且在大多数集合类、System.String 等中“在幕后”使用。

因此,数组已变得更加低级,应用程序程序员将使用它们直接频率较低。

Have arrays lost (some) significance?

Yes. For many tasks requiring a 'table' of items there now are more flexible and useful solutions like List<> and IEnumerable<>.

Have arrays lost their importance?

No. They are the fastest form of storage and they are used 'under the hood' in most of the collection classes, System.String etc.

So, arrays have become more low-level, and an application programmer will be using them directly less often.

孤独陪着我 2024-09-25 22:28:02

抛开其他一切不谈,您提到的许多出色的集合类都是使用数组实现的。您可能没有明确地使用它们,但您正在使用它们的负载,并且您的程序更适合它。这意味着数组必须采用该语言(或者集合是直接使用大量本机代码实现的,这会更糟糕)。

Quite apart from everything else, many of those great collection classes you refer to are implemented using arrays. You might not be using them explicitly, but you're using loads of them and your program is better for it. That means that arrays must be in the language (or that the collections are implemented directly using lots of native code, which would be suckier).

悲凉≈ 2024-09-25 22:28:02

是的?每当我有一个内部维护固定大小的项目集合的类型时,我都会使用数组,因为它的迭代速度最快并且需要最少的内存。如果您不需要这些功能,那么使用 ListQueue 等就没有意义。

Yes? Anytime I have a type which internally maintains a fixed-size collection of items, I use an array as it's the fastest to iterate and requires the least memory. No sense using a List<T>, Queue<T>, etc. if you don't need those features.

时光倒影 2024-09-25 22:28:02

我每天使用数组的两个案例:

  • 图像分析。图像几乎总是 byte[] 或 int[] 数组。
  • 外部硬件通信。大多数设备需要结构完美的数组来发送/接收消息。

Two cases where I work daily with arrays:

  • Image analysis. Images are almost always byte[] or int[] arrays.
  • External hardware communication. Most devices require perfectly structured arrays to send/receive messages.
各空 2024-09-25 22:28:02

这是一个公平的问题,但答案肯定是它们仍然有用。速度是一个原因,固定尺寸的简单性是另一个原因。但我认为最重要的是灵活性。它为您提供了一个很好的基础来设计您自己的集合,如果您需要的话,它有一个简单的数组支持。

It's a fair question, but the answer is definitely that they're still useful. Speed is one reason, simplicity for fixed sizes is another. But I think the most important one is flexibility. It gives you a nice base to design your own collection, backed by a simple array, if you ever needed it.

云淡月浅 2024-09-25 22:28:02

不,数组不会失去它的重要性。

当您提前知道项目的数量时,您可以选择数组,这可以让您非常快速地访问。

2-在图论中,当您存储有关顶点之间链接的信息时,您可以使用比 LinkList 实现更快的数组来实现。

3-一些方法如 string.split 返回数组。

您可以在许多计算机问题中使用这个精彩的静态项目占位符

No , Array will not loose its importance.

As when you know about the no of items in advance , you can go for array which gives you very fast access.

2- In Graph theroy , still when you store information about link between vertex , you can implment using arrays which are more fast than LinkList implementation.

3- Some methods like string.split returns array.

you can use this wonderful static placeholder of items in a verity of computer problems

站稳脚跟 2024-09-25 22:28:02

我使用数组来操作图像,就像在 WritableBitmap 类中一样。

I use arrays to maniulate images like in WritableBitmap class.

咆哮 2024-09-25 22:28:02

我必须告诉您的一件事是,数组是任何编程语言的构建块。如果您想声明一个具有多个元素的存储,数组是您的基本选择。

比如说一个列表。

如果你看到List的定义,它实际上保存了

T[]项,

只要使用Reflector找到List的定义,你会惊讶地发现List实际上是一个Array。在 .NET 中,除 LinkedList 之外的大多数集合基本上都是数组实现。他们使用数组是因为它的存储和检索速度快。

我同意数组有更新或删除的限制,如果您主要强调存储而不是速度,您可能会选择链接列表。

One thing I must tell you Arrays are the building blocks for any programming language. If you want to declare a storage having more than one element, Arrays are the basic option for you.

Say for instance a List.

If you see the definition of List, it actually holds

T[] items

Just use Reflector and find the definition of List, you will be surprised to find out List to be actually an Array. In .NET most of the collection other than LinkedList are basically an Array implementation. They used Array because of its fast storage and retrieval.

I agree that Array has limitation of Update or Remove, if your main emphasis is in storage than speed, you might go for Linked List.

舟遥客 2024-09-25 22:28:02

您认为许多精美系列背后的支持领域是什么?

What do you think the backing field behind many of those fancy collections is?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文