在此示例中使用临时变量还是内联变量?

发布于 2024-07-27 15:20:57 字数 573 浏览 2 评论 0原文

我正在加载 ASP.NET 下拉列表。

这样做有什么好处吗::

    Private Sub LoadSeasonsListbox(ByVal seasons As List(Of Season))
      Dim li As ListItem
      For Each s As Season In seasons
        li = New ListItem(s.SeasonDescription, s.SeasonCodeID)
        frm.SeasonsList.Items.Add(li)
      Next
    End Sub

比这样做:

Private Sub LoadSeasonsListbox(ByVal seasons As List(Of Season))
    For Each s As Season In seasons
        frm.SeasonsList.Items.Add(New ListItem(s.SeasonDescription, s.SeasonCodeID))
    Next
End Sub

I'm loading an ASP.NET dropdown list.

Is there any advantage to doing this::

    Private Sub LoadSeasonsListbox(ByVal seasons As List(Of Season))
      Dim li As ListItem
      For Each s As Season In seasons
        li = New ListItem(s.SeasonDescription, s.SeasonCodeID)
        frm.SeasonsList.Items.Add(li)
      Next
    End Sub

over this:

Private Sub LoadSeasonsListbox(ByVal seasons As List(Of Season))
    For Each s As Season In seasons
        frm.SeasonsList.Items.Add(New ListItem(s.SeasonDescription, s.SeasonCodeID))
    Next
End Sub

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

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

发布评论

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

评论(7

淡淡離愁欲言轉身 2024-08-03 15:20:57

调试时,第一个可以更轻松地检查所添加的 ListItem

第一个宽度也较低,有些人可能会发现更容易阅读(但高度较高,有些人可能会发现更难阅读......)

When debugging, the first makes it easier to examine the ListItem being added.

The first also has a lower width which some may find easier to read (but a higher height which some may find harder to read...)

单身情人 2024-08-03 15:20:57

我认为它们之间没有任何真正的性能差异,也没有正确性差异。

我认为第一个更容易调试,因为您可以轻松地停在 Add 行并查看 li 的内容。

I don't think there is any real performance difference between them, nor correctness difference.

I think the first one is easier to debug, because you can easily stop on the Add line and view the contents of li.

掩饰不了的爱 2024-08-03 15:20:57

我认为1两者实际上都会编译为相同的字节码。 然而,我通常只在使用新变量时才倾向于引入它们。 或者当它使代码更清晰时。 在这种情况下,语句并不是非常复杂,并且可以立即看出您正在做什么,因此使用临时变量可能没有真正的好处。

1这只是一个猜测。 使用 Reflector 或类似工具进行验证。

I think1 that both will actualy compile to the same bytecode. However, I usually only tend to introduce new variables when I am going to use them. Or when it makes the code clearer. In this case the statement is hardly very complex and immediately visibly what you are doing, so there is probably no real benefit to using a temporary variable.

1 It's just a guess. Verify with Reflector or similar.

知你几分 2024-08-03 15:20:57

我最初会使用方法 1 来编写它并进行调试,然后在它与示例数据配合良好时重构为方法 2。

I would originally write it and debug using method 1, then refactor to method 2 once I have it working well with my sample data.

海未深 2024-08-03 15:20:57

您还可以使用 LINQ(和 C#,因为我无法编写 VB.NET 代码;)来完成此操作。

private void LoadSeasonsListbox(IEnumerable<Season> seasons)
{
    frm.SeasonsList.Items.AddRange(seasons
        .Select(s => new ListItem(s.SeasonDescription, s.SeasonCodeID))
        .ToArray());
}

我更喜欢这个解决方案,因为我讨厌这个循环只是复制或翻译对象 - 它们使代码变得非常混乱。 我什至会考虑编写一个扩展方法。

private void LoadSeasonsListbox(IEnumerable<Season> seasons)
{
    frm.SeasonsList.Items.AddRange(
        seasons.ToListItems(s => s.SeasonDescription, s => s.SeasonCodeID));
}

You can also do it using LINQ (and C# because I cannot write VB.NET code ;).

private void LoadSeasonsListbox(IEnumerable<Season> seasons)
{
    frm.SeasonsList.Items.AddRange(seasons
        .Select(s => new ListItem(s.SeasonDescription, s.SeasonCodeID))
        .ToArray());
}

I prefer this solution because I hate this loops just copying or translating objects - they clutter the code so much. I would even think about writing an extension method.

private void LoadSeasonsListbox(IEnumerable<Season> seasons)
{
    frm.SeasonsList.Items.AddRange(
        seasons.ToListItems(s => s.SeasonDescription, s => s.SeasonCodeID));
}
伴我心暖 2024-08-03 15:20:57

与第一个相比,第二个的唯一优点是它消除了仅使用一次的变量。 重构中的某些东西有时是可取的。

另一方面,使用这样的中间变量可以通过稍微分解所涉及的步骤来使代码更具可读性。

The only advantage of the seconf over the first is that it eliminates a variable which is only used once. Something that in refactoring is sometimes desirable.

On the other hand, using an intermediatory variable like this can make the code a little more readable by breaking the steps involved down a bit.

燃情 2024-08-03 15:20:57

我认为这两个例子是等效的,但我更喜欢后者。

I think both examples are equivalent, but I do prefer the latter.

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