在此示例中使用临时变量还是内联变量?
我正在加载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
调试时,第一个可以更轻松地检查所添加的
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...)
我认为它们之间没有任何真正的性能差异,也没有正确性差异。
我认为第一个更容易调试,因为您可以轻松地停在 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.
我认为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.
我最初会使用方法 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.
您还可以使用 LINQ(和 C#,因为我无法编写 VB.NET 代码;)来完成此操作。
我更喜欢这个解决方案,因为我讨厌这个循环只是复制或翻译对象 - 它们使代码变得非常混乱。 我什至会考虑编写一个扩展方法。
You can also do it using LINQ (and C# because I cannot write VB.NET code ;).
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.
与第一个相比,第二个的唯一优点是它消除了仅使用一次的变量。 重构中的某些东西有时是可取的。
另一方面,使用这样的中间变量可以通过稍微分解所涉及的步骤来使代码更具可读性。
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.
我认为这两个例子是等效的,但我更喜欢后者。
I think both examples are equivalent, but I do prefer the latter.