VB.NET 使用多个 USING 语句与仅使用一个 USING 语句之间的区别
以下哪种方法是最好的方法?或者两者效果相同?
Dim carrierName As String
Dim someotherName As String
Dim anotherOne As String
Using oDa As New MyCompany.DataAccess.MyModule
carrierName = oDa.GetCarrierName(itemNumber)
End Using
Using oDa As New MyCompany.DataAccess.MyModule
someotherName = oDa.GetSomeOtherName(itemNumber,1)
End Using
Using oDa As New MyCompany.DataAccess.MyModule
anotherOne = oDa.GetAnotherName("somevalue")
End Using
或者
Using oDa As New MyCompany.DataAccess.MyModule
carrierName = oDa.GetCarrierName(itemNumber)
someotherName = oDa.GetSomeOtherName(itemNumber,1)
anotherOne = oDa.GetAnotherName("somevalue")
End Using
Which one is best approach out of the following? Or Both has same effect ?
Dim carrierName As String
Dim someotherName As String
Dim anotherOne As String
Using oDa As New MyCompany.DataAccess.MyModule
carrierName = oDa.GetCarrierName(itemNumber)
End Using
Using oDa As New MyCompany.DataAccess.MyModule
someotherName = oDa.GetSomeOtherName(itemNumber,1)
End Using
Using oDa As New MyCompany.DataAccess.MyModule
anotherOne = oDa.GetAnotherName("somevalue")
End Using
OR
Using oDa As New MyCompany.DataAccess.MyModule
carrierName = oDa.GetCarrierName(itemNumber)
someotherName = oDa.GetSomeOtherName(itemNumber,1)
anotherOne = oDa.GetAnotherName("somevalue")
End Using
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,一个版本将创建三个
MyModule
实例,另一个版本只会创建一个。如果不了解MyModule
的更多信息,我们就无法分辨出区别是什么。第二种方法对我来说看起来更干净,但在不知道语义差异的情况下,很难说它绝对更好。
Well, one version will create three
MyModule
instances, the other will only create one. We can't tell what the difference is without knowing more aboutMyModule
.The second approach looks cleaner to me, but without knowing the semantic differences, it's hard to say that it's definitely better.
在第一个中,您将创建和处置 MyModule 的 3 个实例,而在第二个中,您仅创建和处置 1 个实例。所以第二种方法更好。它也更干净、更简单。
In the first, you are creating and disposing of 3 instances of MyModule while in the second you are only creating and disposing 1 instance. So the second approach is better. It's cleaner and more straightforward too.
这两种方法本质上是不同的,但最终结果取决于您的 MyModule 实现。
第二个似乎更好,因为它只创建和管理一个 MyModule 对象,特别是当 MyModule 的创建和处置成本很高时。
如果您的 MyModule 不允许同一实例的多个请求,则第一个可能是必要的。但如果是cas的话。哼...对我来说这看起来像是一个错误。
The two approachs are fondamentally different, but the end result depend on your MyModule implementation.
The second one seems better, as it creates and manages only one MyModule object, especially if MyModule is costly to create and to dispose.
The first one can be necessary if your MyModule doesn't allow multiple requests with the same instance. But if it's the cas. hem... it would look like a bug to me.
大多数情况下后者更好。使用只是
第一个方法的快捷方式,当每个方法分配大量需要立即清理的内存时可以使用,但我不认为这是你的情况。
顺便说一句:你似乎把 c# 和 Visual Basic 搞错了
The latter is better in most cases. Using is just shortcut for
First method could be used when every method allocates huge amount of memory that will need to be cleaned right away, but I don't think it is your case.
btw: you seem to have mistaken c# with visual basic