开发者

automatically expanding array in .NET?

Does .NET have anything similar to Perl arrays, which are indexed numerically but automatically expand as needed? It would work like this:

var x = new DreamArray<string>();

x[6] = "foo";  // x automatica开发者_StackOverflow中文版lly has 7 elements
x[10] = "bar"; // now it has 11


Use a List<string> instead of an array. You will have to call List.Add("item") to add to the list though.


No but it could be easily emulated with:

class MagicArray<T> : Dictionary<int, T> {}


Hash Tables & ArrayLists are the first 2 things to come to mind. They're not used exactly the same, though you could use a hashtable in a pretty similar manner.

See C# Collections for usage, examples, and more ideas


You could easily write your own. You would have to implement the ICollection interface and then aggregate a standard List, etc. But, in the indexer property, if the index is greater than the Capacity, simply change the Capacity property to the appropriate size.


Lists would be the closest. Just use List()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜