如何“使用"?语句从 C# 转换为 VB? [英] How does the "Using" statement translate from C# to VB?

查看:27
本文介绍了如何“使用"?语句从 C# 转换为 VB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

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?

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

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

推荐答案

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

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"

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

编辑

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

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.

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

这篇关于如何“使用"?语句从 C# 转换为 VB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆