The using statement is used to set one or more than one resource. These resources are executed and the resource is released. The statement is also used with database operations.
The main goal is to manage resources and release all the resources automatically.
Let us see an example wherein “A” would print first since the SystemResource is allocated first.
using System; using System.Text; class Demo { static void Main() { using (SystemResource res = new SystemResource()) { Console.WriteLine("A"); } Console.WriteLine("B"); } } class SystemResource : IDisposable { public void Dispose() { Console.WriteLine("C"); } }
A C B