[Switch] C # using three usage methods,
Original address.
1. using command
Using + namespace. This method has basically been used by C #. The advantage is that you do not need to specify a detailed namespace when writing code.
using System.Windows.Media;using System.Windows.Media.Imaging;
2. using statement
It is used to simplify resource release and is valid within a certain range. Besides this range, IDisposable is automatically called to release it. Of course, not all classes are applicable, only classes that implement the IDisposable interface can be used.
Using (SqlConnection conn = new SqlConnection ("Data Source = .; initial Catalog = imageprocess; Integrated Security = True ") {conn. open (); using (SqlCommand cmd = conn. createCommand () {cmd. commandText = "select count (*) from [user]"; int I = (int) cmd. executeScalar (); MessageBox. show (I. toString ();} // SqlCommand is automatically released when the bracket ends} // SqlConnection is automatically released when the bracket ends
Besides using, try catch is also acceptable.
3. using alias
Using + alias = contains the specific types of detailed namespace information. This method has the advantage that when the same cs references two different namespaces, however, both namespaces contain the same name type. When this type is required, the detailed namespace method should be used in every place to distinguish the types with the same name. The alias method is more concise. You can declare the alias for the class you use. Note: This does not mean that the two names are repeated. If you use an alias for one of them, you do not need to use the alias for the other. If you want to use both of them, you need to use using to define the alias for both of them.
Using System; using aClass = NameSpace1.MyClass; using bClass = NameSpace2.MyClass; namespace NameSpace1 {public class MyClass {public override string ToString () {return "You are in NameSpace1.MyClass ";}}} namespace NameSpace2 {class MyClass {public override string ToString () {return "You are in NameSpace2.MyClass" ;}} namespace testUsing {using NameSpace1; using NameSpace2; // <summary>/ // Summary of Class1. /// </Summary> class Class1 {/// <summary> /// main entry point of the application. /// </Summary> [STAThread] static void Main (string [] args) {// TODO: add code here to start the application // aClass my1 = new aClass (); Console. writeLine (my1); bClass my2 = new bClass (); Console. writeLine (my2); Console. writeLine ("Press any key"); Console. read ();}}}
So far, the third case has not been used by individuals.