System.Collections.LinkedList在C#中有用吗? [英] Is System.Collections.LinkedList Useful in C#?

查看:66
本文介绍了System.Collections.LinkedList在C#中有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,C#中的引用"类型大致类似于C ++中的指针.如果真是这样,我想弄清楚System.Collections.LinkedList是否真的有任何用途.如果我使用C ++,如果我的数据类型将仅仅是一个指针,则不会产生链表的内存开销.我相信数组上链表的最大卖点之一是不需要连续内存.但是,当处理指针时,现代计算机分配非常大的指针数组并不十分困难.我想仍然需要一个结构比类更多的程序.

有没有人曾经使用过LinkedList?是真的有必要吗?或者,如果您来自C/C ++背景,您是否只是陷入了这种思维模式?

As I understand it, a "reference" type in C# is roughly analogous to a pointer in C++. If that is the case, I''m trying to figure out if System.Collections.LinkedList really serves any purpose. If I were working in C++, I would not incur the memory overhead of a linked list if my data type was going to be merely a pointer. I believe one of the big selling points of linked lists over arrays is the non-need for consecutive memory. But when dealing with pointers, it''s not terribly difficult for modern computers to allocate even very large arrays of pointers. I suppose the need could still be there for a program that had a lot more structs than classes.

Has anyone out there ever used LinkedList? Was it really necessary, or did you just find yourself in that thought pattern if you came from a C/C++ background?

推荐答案

是的,它完全可以满足以下目的:此条件很重要:快速插入/删除操作,其性能不取决于列表的长度.

您对链表的价值的猜测几乎是不正确的.首先,从所讨论的算法的角度来看,托管引用和指针之间没有本质区别.它们之间的差异(被引用对象在物理内存中重新定位的能力)对于算法是绝对透明的.同样,当涉及链表或常规列表时,结构和类之间也没有太大的区别.请记住,结构成员可以是引用类型.但是,关于链表在某些特殊情况下的好处的想法是正确的:当结构的实例很大且成员主要是引用类型时,就是这种情况(递归)—在这种情况下,可以减轻对大的连续内存的需求链接列表.但这不是唯一的好处,也不是链表的主要好处.

—SA
Yes, it perfectly serves a purpose where this criteria is important: fast insert/remove operation, with performance not depending on the length of the list.

Your speculations on the value of linked list are hardly correct. First, from the standpoint of the algorithms in question, there are no essential difference between managed references and pointers. The difference between them (the ability of referenced objects to relocate in physical memory) is absolutely transparent for the algorithm. Also, when it comes to the linked list or a regular list, there is no so big difference between structures and classes. Remember that structure members can be reference types. However, the idea about benefits of linked list for some special case is correct: this is the case when the instances of structures are big and the members are predominantly reference type, recursively — in this case the need for big consecutive memory can be relaxed with the linked lists. But this is not the only benefit, and not the main benefit of linked lists.

—SA


这篇关于System.Collections.LinkedList在C#中有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆