如何在C#中展开数组而不使用new关键字 [英] How do I expand an array in C# without using the new keyword

查看:413
本文介绍了如何在C#中展开数组而不使用new关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中有一个char数组。

I have a char array in C#.

var arr = new char[3] { 'a','b','c' };

如何在不创建新数组的情况下在其末尾添加空格?

How do I add spaces to the end of it without creating a new array?

结果: arr = {'a','b','c','',',''};

这听起来类似于VB.NET的 ReDim 。但是我不确定那不是我想要的。
我想保留其中的元素,而不是在后台实例化新数组。

This might sound similar to VB.NET's ReDim. But I'm not sure that is what I want either. I want to preserve the elements inside of it and not instantiate a new array behind the scenes.

只有通用集合和ArrayList才能做到这一点吗?

Is this only possible with Generic Collections and ArrayList?

谢谢

推荐答案

不,使用通用数组无法实现否则,.. AFAIK,无法动态调整数组大小。请使用列表

No, this is not possible using an array, generic or otherwise.. AFAIK, there is no way to dynamically resize an array. Use a List instead.

正如Martin在评论中指出的那样,即使 List 类在其内部实现中也使用数组。如果您希望真正能够动态调整数据结构的大小而无需重新初始化,则必须实现自己的版本。链表

As Martin pointed out in the comments, even the List class uses an array in its internal implementation. If you want to truly be able to dynamically resize a data structure without reinitializing it, you must implement your own version of a linked list.

System.Collections.Generic 包含名为 LinkedList ,它表示一个双向链接列表(表示每个节点都引用了下一个和第二个链接前一个节点),但我不确定其内部实现是否使用数组。

System.Collections.Generic contains a class called LinkedList that represents a doubly-linked list (meaning that each node has a reference to both the next and the previous node), but I'm not sure if its internal implementation uses an array..

这篇关于如何在C#中展开数组而不使用new关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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