我有 3 种方法来获取数组的 ubound

发布于 2024-11-08 03:08:19 字数 370 浏览 0 评论 0原文

我有这 3 段代码:

 For x = 0 To sections.Length - 1
        'work
    Next
    For x = 0 To UBound(sections)
        'work
    Next
    For x = 0 To sections.GetUpperBound(0)
        'work
    Next

我已经思考了大约 10 分钟,但我仍然无法确定其中一种选择相对于另一种选择的优势到底是什么。

我想知道是否有人真正考虑过这个问题并愿意分享他们的见解。基本上我想知道哪个是“更好”(你可以引用清晰度、容易重构、性能、可维护性、良好的标准,任何使它“更好”的东西)

I've got this 3 piece of code:

 For x = 0 To sections.Length - 1
        'work
    Next
    For x = 0 To UBound(sections)
        'work
    Next
    For x = 0 To sections.GetUpperBound(0)
        'work
    Next

I've been thinking for about 10 minutes but I still can't decide what exactly are the advantages of 1 choice over the other.

I was wondering if anyone has actually given this some thought and would like to share their insights. Basically I would like to know which is "better" (you can quote clarity, easy refactoring, performance, maintainability, good standard, anything that makes it "better")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-残月青衣踏尘吟 2024-11-15 03:08:19

我假设您需要在循环内使用索引 - 否则,只需使用 For Each 循环即可。

在我看来,1 是一维数组的惯用方式。这是最接近您在 C# 中看到的正常方式:

for (int x = 0; x < array.Length; x++)

不幸的是,如今 VB 具有这种轻微的双重性格,无论它是从零还是从一开始。 .NET 通常是一个零基础的平台,而您在这里正遭受着痛苦;围绕从零开始的 API 设计的语言可能会使“包含下限;排除上限”模式变得更简单 - 而 VB 在两端都包含,这就是为什么你需要“-1” ”。

2 看起来像是VB特有的函数;我会尽量避免这些,除非它们具有显着的优势,因为这会使非 VB 程序员(例如 C# 程序员)更难理解代码。在许多情况下,这只是一个非常小的问题,但当它没有真正的优势时,我会坚持使用“常规”.NET 方法。

3 实际上是为多维(矩形)数组设计的 - 0 表示您想要第一个维度。当您处理一维数组时,它会无缘无故地增加额外的复杂性。

第一种方法可能也是性能最好的 - JIT 识别这种模式,并且能够删除循环内的一些数组边界测试(它知道 x 在数组的边界)。现在 UBound 可能会编译为具有相同属性的 IL - 我不确定。我怀疑它调用了 Microsoft.VisualBasic 程序集中的方法,而 JIT 不太可能知道该方法。

I'm assuming you need to use the index within the loop - otherwise, just use a For Each loop instead.

1 is the idiomatic way for a single-dimensional array, in my view. It's the closest to the normal way you'd see in C#:

for (int x = 0; x < array.Length; x++)

It's unfortunate that VB has this slight dual personality these days in terms of whether it's zero or one-based. .NET is generally a zero-based platform, and you're suffering for it here; languages designed around zero-based APIs are likely to make the "inclusive lower-bound; exclusive upper-bound" pattern simpler - whereas VB is inclusive at both ends, which is why you need the "-1".

2 looks like it's a VB-specific function; I would try to avoid these unless they present significant advantages, as it will make it harder for non-VB programmers (e.g. C# programmers) to understand the code. That's only a very minor concern in many cases, but when there are no real advantages to it, I'd stick with "regular" .NET approaches.

3 is really designed for multi-dimensional (rectangular) arrays - the 0 there is saying that you want the first dimension. When you're dealing with single-dimensional arrays it's adding extra complexity for no good reason.

It's possible that the first approach is also the most performant - the JIT recognizes this pattern and is able to remove some array bounds tests within the loop (it knows that x is within the bounds of the array). Now UBound may compile to IL that has the same property - I'm not sure. I suspect it makes a call to a method within the Microsoft.VisualBasic assembly, which the JIT is less likely to have knowledge of.

裂开嘴轻声笑有多痛 2024-11-15 03:08:19

它基本上取决于您正在做的事情的语义。选择最能清楚地表达您在代码中所做的事情的一项。

It basically depends on the semantics of what you are doing. Choose whichever one most clearly expresses what you're doing in the code.

天涯沦落人 2024-11-15 03:08:19

我更喜欢第三个,因为它最清楚您正在测量的内容 - 您将获得数组“部分”的第一个索引的上限。其他人也描述了同样的事情,但我认为他们做得不太清楚。

I prefer the third one because it's most clear what you're measuring - you're getting the upper bound of the first index of the array "sections". The others describe the same thing, but I don't think they do it as clearly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文