在VB中重新编译为C#转换 [英] redim in VB to C# Conversion

查看:93
本文介绍了在VB中重新编译为C#转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,有没有办法在C#中模拟VBs redim函数?



谢谢

解决方案

< blockquote>在VB中,redim用于调整数组大小。在C#中没有必要使用List,其大小根据需要动态增加,请参阅 list [ ^ ]


对于单维数组,是的,您可以使用以下命令直接模拟VB .NET的'redim和'redim preserve'功能:Array.Resize(ref NameOFArray,#newSize);



VB。 NET的'redim允许你改变多维数组中任何维度的大小;要在C#中实现这一点,需要编写一个自定义方法来构建所需维数(排名)的新数组,并复制正在复制的数组中的相应值。



请参阅:C#Array.Resize:[ ^ ]。



在上面链接的文章中,您将找到用于在C#中模拟VB'redim的示例代码使用多维数组。



请记住如果您有对现有数组的引用,然后在C#中调整数组大小,那么引用没有更新/更改!我建议你仔细研究一下Jon Skeet的例子:[ ^ ]。



是否应该更改.NET中Array对象的使用以使用基于通用列表的结构的问题是一个有趣的问题。对我来说,答案是不;我相信有很多情况下使用Arrays是非常有效的,也很有用。



但是,imho,你应该真正研究不同的观点;我建议您查看此Google搜索的前六个结果:[ ^ ]。


Hello everyone, is there a way to simulate VBs redim function in C#?

Thanks

解决方案

In VB, redim is used to resize array. There is no need for that in C#, you use a List whose size is dynamically increased as required, see list[^]


For a single-dimension Array, yes, you can directly simulate VB .NET's 'redim, and 'redim preserve,' functionality by using: Array.Resize(ref NameOFArray, #newSize);

VB .NET's 'redim allows you to change the size of any dimension in a multi-dimensional Array; to achieve that in C# requires you write a custom method to build a new Array of the required number of dimensions (rank), and copy over the appropriate values from the array-being-copied.

See: C# Array.Resize: [^].

In the article linked to above, you will find sample code for simulating VB 'redim in C# with a multi-dimensional Array.

Do keep in mind that if you have references to an existing Array, and then resize the Array in C#, the references are not updated/changed ! I suggest you carefully study Jon Skeet's example of this, here: [^].

The issue of whether or not any use of the Array object in .NET should be changed to use a Generic List based structure is an interesting one. For me, the answer is "no;" I believe there are many situations where using Arrays is quite efficient, and useful.

But, imho, you should really examine the different viewpoints on this; I suggest you examine the first six results of this Google search: [^].


这篇关于在VB中重新编译为C#转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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