HTML页面把list转成array,c# – 将Collection转换为Array或List的快速方法?

 2023-09-09 阅读 15 评论 0

摘要:对于我需要传递给只接受数组或列表的方法的每个* Collection(HtmlNodeCollection,TreeNodeCollection,CookieCollection等)类实例(例如,不应该有一个接受set转为list、TreeView中的TreeNodeCollection的方法?)我必须写一个像这样的扩展方法:public static TreeN

对于我需要传递给只接受数组或列表的方法的每个* Collection(

HtmlNodeCollection,TreeNodeCollection,CookieCollection等)类实例(例如,不应该有一个接受

set转为list、TreeView中的TreeNodeCollection的方法?)我必须写一个像这样的扩展方法:

public static TreeNode[] ToArray(this TreeNodeCollection nodes)

{

set集合转list、TreeNode[] arr = new TreeNode[nodes.Count];

nodes.CopyTo(arr, 0);

return arr;

list array?}

或者遍历整个集合,将项添加到输出列表,然后将输出列表转换为数组:

public static TreeNode[] ToArray(this TreeNodeCollection nodes)

json转list?{

var output = new List();

foreach (TreeNode node in nodes)

java object转list?output.Nodes(node);

return output.ToArray();

}

所以,我的问题是:

我经常需要这种扩展方法.如果列表很大,它可能会分配大量内存,通常是这样.为什么我不能只获得这个* Collection类使用的内部数组的引用(而不是复制),这样我就不需要使用那些扩展并执行这些内存分配了?甚至提供ToArray()方法.我们不需要知道它在最后一种情况下使用的内部实现或数组.

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/28323.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息