C # Three using methods,

Source: Internet
Author: User

[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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.