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
?
Edit:
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.
Its important to point out that using is actually compiled into various lines of code, similar to lock, etc.
From the C# language specification.... A using statement of the form
corresponds to one of two possible expansions. When
ResourceType
is a value type, the expansion isOtherwise, when ResourceType is a reference type, the expansion is
(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.
That would be something like this:
Seems like
using
(C#) andUsing
(VB) have an extremely important difference. And at least for me now, it can defeat the purpose ofUsing
.You'll get NullReferenceException as in VB
Using
redefines the member class variable while in C# it doesn't!Of course, maybe I missing something..
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"
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.
The key point is that the class being "used" must implement the IDisposable interface.