For example:
BitmapImage bitmap = new BitmapImage();
byte[] buffer = GetHugeByteArray(); // from some external source
using (MemoryStream stream = new Memor
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
using (ResourceType resource = expression) statement
corresponds to one of two possible expansions. When ResourceType is a value type, the expansion is
{
ResourceType resource = expression;
try {
statement;
}
finally {
((IDisposable)resource).Dispose();
}
}
Otherwise, when ResourceType is a reference type, the expansion is
{
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.