VB.NET 使用多个 USING 语句与仅使用一个 USING 语句之间的区别

发布于 2024-10-16 18:00:01 字数 720 浏览 2 评论 0原文

以下哪种方法是最好的方法?或者两者效果相同?

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 技术交流群。

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

发布评论

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

评论(4

羅雙樹 2024-10-23 18:00:01

好吧,一个版本将创建三个 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 about MyModule.

The second approach looks cleaner to me, but without knowing the semantic differences, it's hard to say that it's definitely better.

病女 2024-10-23 18:00:01

在第一个中,您将创建和处置 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.

老旧海报 2024-10-23 18:00:01

这两种方法本质上是不同的,但最终结果取决于您的 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.

欢你一世 2024-10-23 18:00:01

大多数情况下后者更好。使用只是

var oDa = new MyCompany.DataAccess.MyModule();
try {
    carrierName = oDa.GetCarrierName(itemNumber)   
    someotherName = oDa.GetSomeOtherName(itemNumber,1)   
    anotherOne = oDa.GetAnotherName("somevalue")
}
finally {
    oDa.Dispose();
}

第一个方法的快捷方式,当每个方法分配大量需要立即清理的内存时可以使用,但我不认为这是你的情况。

顺便说一句:你似乎把 c# 和 Visual Basic 搞错了

The latter is better in most cases. Using is just shortcut for

var oDa = new MyCompany.DataAccess.MyModule();
try {
    carrierName = oDa.GetCarrierName(itemNumber)   
    someotherName = oDa.GetSomeOtherName(itemNumber,1)   
    anotherOne = oDa.GetAnotherName("somevalue")
}
finally {
    oDa.Dispose();
}

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

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