The Using keyword in C # has two main uses:
1. Using as directive:
(1) used to introduce namespaces into the current unit, for example: using System.Windows.Forms;
(2) Create an alias for the existing namespace, for example: using Form = System.Windows.Forms;
2. Using as statement: Defines a code block scope that frees resources at the end of the code block.
(1) Declare an object before the using statement:
1 New Class (); 2 using (C1) 3 {4 // Use C1 5 }
(2) Declaring an object in a using statement, which must be declared inside a using statement when more than one object is to be used:
1 using New New Class ()) 2 {3 // Use c1, C2 4 }
During the program compilation phase, the compiler automatically builds the using statement into a try-catch-finally statement and calls the object's Dispose method in the finally block to clean up the resource.
Using keyword Usage summary in C #