“Using”语句如何从 C# 转换为 VB?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/887831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 08:30:58  来源:igfitidea点击:

How does the "Using" statement translate from C# to VB?

c#.netvb.netvisual-studio-2005.net-2.0

提问by Daniel

For example:

例如:

BitmapImage bitmap = new BitmapImage();

byte[] buffer = GetHugeByteArray(); // from some external source
using (MemoryStream stream = new MemoryStream(buffer, false))
{
    bitmap.BeginInit();
    bitmap.CacheOption = BitmapCacheOption.OnLoad;
    bitmap.StreamSource = stream;
    bitmap.EndInit();
    bitmap.Freeze();
}

Can you tell me any more about using?

你能告诉我更多关于using吗?

Edit:

编辑:

As was discussed in the comments of JaredPar's post, this question is more concerned with an implementation of Usingin VS2003. It was pointed out that Usingwas not introduced until .NET 2.0 (VS2005). JaredPar posted an equivalent workaround.

正如 JaredPar 帖子的评论中所讨论的,这个问题更关心UsingVS2003 中的实现。有人指出,Using直到 .NET 2.0 (VS2005) 才引入。JaredPar 发布了一个等效的解决方法。

采纳答案by JaredPar

Using has virtually the same syntax in VB as C#, assuming you're using .NET 2.0 or later (which implies the VB.NET v8 compiler or later). Basically, just remove the braces and add a "End Using"

假设您使用的是 .NET 2.0 或更高版本(这意味着 VB.NET v8 编译器或更高版本),Using 在 VB 中的语法与 C# 几乎相同。基本上,只需移除大括号并添加“结束使用”

Dim bitmap as New BitmapImage()
Dim buffer As Byte() = GetHugeByteArrayFromExternalSource()
Using stream As New MemoryStream(buffer, false)
    bitmap.BeginInit()
    bitmap.CacheOption = BitmapCacheOption.OnLoad
    bitmap.StreamSource = stream
    bitmap.EndInit()
    bitmap.Freeze()
End Using

You can get the full documentation here

您可以在此处获取完整文档

EDIT

编辑

If you're using VS2003 or earlier you'll need the below code. The using statement was not introduced until VS 2005, .NET 2.0 (reference). Thanks Chris!. The following is equivalent to the using statement.

如果您使用的是 VS2003 或更早版本,则需要以下代码。using 语句直到 VS 2005, .NET 2.0(参考)才被引入。谢谢克里斯!。以下等效于 using 语句。

Dim bitmap as New BitmapImage()
Dim buffer As Byte() = GetHugeByteArrayFromExternalSource()
Dim stream As New MemoryStream(buffer, false)
Try
    bitmap.BeginInit()
    bitmap.CacheOption = BitmapCacheOption.OnLoad
    bitmap.StreamSource = stream
    bitmap.EndInit()
    bitmap.Freeze()
Finally
    DirectCast(stream, IDisposable).Dispose()
End Try

回答by Bojan Resnik

That would be something like this:

那将是这样的:

Dim bitmap As New BitmapImage()
Dim buffer As Byte() = GetHugeByteArray()
Using stream As New MemoryStream(buffer, False)
    bitmap.BeginInit()
    bitmap.CacheOption = BitmapCacheOption.OnLoad
    bitmap.StreamSource = stream
    bitmap.EndInit()
    bitmap.Freeze()
End Using

回答by LunaCrescens

The key point is that the class being "used" must implement the IDisposable interface.

关键是被“使用”的类必须实现 IDisposable 接口。

回答by Allen Rice

Its important to point out that using is actually compiled into various lines of code, similar to lock, etc.

需要指出的是,using实际上是编译成各种代码行,类似于lock等。

From the C# language specification.... A using statement of the form

来自 C# 语言规范.... 表单的 using 语句

using (ResourceType resource = expression) statement

corresponds to one of two possible expansions. When ResourceTypeis a value type, the expansion is

对应于两种可能的扩展之一。当ResourceType是值类型时,扩展为

{
    ResourceType resource = expression;
    try {
        statement;
    }
    finally {
        ((IDisposable)resource).Dispose();
    }
}

Otherwise, when ResourceType is a reference type, the expansion is

否则,当 ResourceType 是引用类型时,扩展为

{
    ResourceType resource = expression;
    try {
        statement;
    }
    finally {
        if (resource != null) ((IDisposable)resource).Dispose();
    }
}

(end language specification snippet)

(结束语言规范片段)

Basically, at compile time its converted into that code. There is no method called using, etc. I tried to find similar stuff in the vb.net language specification but I couldn't find anything, presumably it does the exact same thing.

基本上,在编译时将其转换为该代码。没有称为 using 等的方法。我试图在 vb.net 语言规范中找到类似的东西,但我找不到任何东西,大概它做了完全相同的事情。

回答by Alex

Seems like using(C#) and Using(VB) have an extremely important difference. And at least for me now, it can defeat the purpose of Using.

似乎using(C#)和Using(VB)有一个极其重要的区别。至少对我现在来说,它可以打败Using.

Imports System.IO
Class Program

    Private Shared sw As StreamWriter

    Private Shared Sub DoSmth()
        sw.WriteLine("foo")
    End Sub

    Shared Sub Main(ByVal args As String())
        Using sw = New StreamWriter("C:\Temp\data.txt")
            DoSmth()
        End Using
    End Sub
End Class

You'll get NullReferenceException as in VB Usingredefines the member class variable while in C# it doesn't!

你会得到 NullReferenceException,因为在 VB 中Using重新定义了成员类变量,而在 C# 中则没有!

Of course, maybe I missing something..

当然,也许我错过了一些东西..