.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda LINQ Parse Path Process Property Regex Replace Sort Split Static StringBuilder Substring Switch Tuple


C#: .NET
Using-keyword

Using. The using block helps manage resources. Conceptually it protects the whole system's resources by specifying the scope of the usage of the resource. The using statement is combined with a type that implements IDisposable.

Interface
Main method

Example. To begin, this little program defines a class called SystemResource. The class implements the IDisposable interface and the required Dispose method. In the Main method, we wrap the SystemResource instance inside a using statement.

Based on:

.NET 4.5

C# program that demonstrates using statement

using System;
using System.Text;

class Program
{
    static void Main()
    {
	// Use using statement with class that implements Dispose.
	using (SystemResource resource = new SystemResource())
	{
	    Console.WriteLine(1);
	}
	Console.WriteLine(2);
    }
}

class SystemResource : IDisposable
{
    public void Dispose()
    {
	// The implementation of this method not described here.
	// ... For now, just report the call.
	Console.WriteLine(0);
    }
}

Output

1
0
2
Question

What happens when the program executes? First, the SystemResource instance is allocated upon the managed heap. Next, the "1" is written. Third, the "0" is written because the Dispose method is invoked. And finally, the "2" is printed.

Then: As demonstrated, the Dispose method is called immediately when control flow exits the using block.


Programming tip

Example 2. It is possible to nest multiple using statements one after another. You do not need to use any curly brackets in this case. In the second or further using statements, you can use the variables declared in previous using statements as well.

C# program that nests using blocks

using System;

class Program
{
    static void Main()
    {
	using (SystemResource resource1 = new SystemResource())
	using (SystemResource resource2 = new SystemResource())
	{
	    Console.WriteLine(1);
	}
    }
}

class SystemResource : IDisposable
{
    public void Dispose()
    {
	Console.WriteLine(0);
    }
}

Cover-logo

Discussion. It is most common to use using with types that are already defined in the .NET Framework. Some of these types include StreamReader, StreamWriter, BinaryReader, BinaryWriter, or even DataTable.

StreamReaderStreamWriterBinaryReaderBinaryWriterDataTable

To do this, simply use the resource acquisition expression for the type inside the using statement. You do not need to implement any interfaces. Many types implement already the Dispose method.


Performance

Performance. The using statement affects performance. In some places, using statements are not necessary because the objects do not touch managed resources. We explore this further. We benchmark using statements on objects that do not require them.

First: This method version that has several using statements. This method receives a byte array, which is a managed type.

Note: All the streams are created from byte arrays, so they cannot be linked to unmanaged resources like the disk.

Decompress GZIP
Version with using statements: C#

static byte[] Decompress1(byte[] gzip)
{
    using (MemoryStream mem = new MemoryStream(gzip))
    using (GZipStream stream = new GZipStream(mem,
    CompressionMode.Decompress))
    {
	const int size = 4096;
	byte[] buffer = new byte[size];
	using (MemoryStream memory = new MemoryStream())
	{
	    int count = 0;
	    do
	    {
		count = stream.Read(buffer, 0, size);
		if (count > 0)
		{
		    memory.Write(buffer, 0, count);
		}
	    }
	    while (count > 0);
	    return memory.ToArray();
	}
    }
}

Next, we see a version that has those three using blocks removed. This is safe because they were not created using unmanaged resources. In the intermediate language, this eliminates a lot of complexity and three try-finally constructs.

Version without using statements: C#

static byte[] Decompress2(byte[] gzip)
{
    GZipStream stream = new GZipStream(new MemoryStream(gzip),
	CompressionMode.Decompress);
    const int size = 4096;
    byte[] buffer = new byte[size];
    MemoryStream memory = new MemoryStream();
    int count = 0;
    do
    {
	count = stream.Read(buffer, 0, size);
	if (count > 0)
	{
	    memory.Write(buffer, 0, count);
	}
    }
    while (count > 0);
    return memory.ToArray();
}
GZIP compression

Third, we look at the program that loads a GZIP file from the disk and stores in a byte array. Note that the File.ReadAllBytes method does use the using construct in it. This is necessary because it opens an unmanaged resource (a file).

Note: The harness decompresses the file 200,000 times. To test memory usage, one of the two Decompress method calls can be commented out.

C# program that benchmarks using statements

using System.IO;
using System.IO.Compression;

class Program
{
    const int _max = 200000;
    static void Main()
    {
	byte[] array = File.ReadAllBytes("C:\\t2\\_default.gz");
	Decompress1(array);
	Decompress2(array);

	for (int i = 0; i < _max; i++)
	{
	    Decompress1(array);
	}
	for (int i = 0; i < _max; i++)
	{
	    Decompress2(array);
	}
    }
}

This section provides information

Results. We see the results to the experiment. Each of the methods took about the same amount of time to execute (9 seconds). This is dependent on the file size. However, the Decompress2 method used consistently less memory.

Thus: The unneeded using statements ended up wasting memory. This could be explained by the method size bloat and the increased complexity.

Benchmark results

Decompress1: Has 3 using statements.
Decompress2: Has 0 using statements.

Decompress1 Memory: 4852 K, 4804 K, 4852 K
Decompress2 Memory: 4768 K, 4752 K, 4784 K

C# programming language

Summary. The using statement provides a syntactic sugar for invoking the Dispose method found in the IDisposable interface implementation. This article does not describe a useful implementation of Dispose.

Review: The using statement interacts with Dispose. It affects the path of the control flow in the virtual execution engine.