asp.net use keywords in C #

Source: Internet
Author: User

For using keywords, it may be familiar to introduce the package features such as code: using System;


Another method is similar to the one used above, except that we can specify an alias, code such as:

Using INet = System.Net;

The last usage is primarily used to release the resources occupied by the object, as follows: using (CLASSC c = new CLASSC ())

{

C.print ();

}


The above code guarantees that the program executes the Dispose method of object C when exiting the using block, even if return exits the function or throws an exception in the using block.

Here CLASSC must implement the interface IDisposable, or it will quote the following compile error code: ' Usingtest.classa ': type used in a using statement must is implicitly Convertible to ' System.IDisposable '


From the code above, the using statement attempts to implicitly convert object C to an IDisposable interface.

To sum up, the using statement block for a C object is equivalent to the following block: {

CLASSC C = new CLASSC ();

Try

{

C.print ();

}

Finally

{

((IDisposable) c). Dispose ();

}

}


Notice that the outermost brace here is also necessary, because the braces limit the active space of object C, the above program accomplishes exactly the same function as the program using the using statement, and the only difference here is that the compiler does a type check at compile time when using a using keyword. The above code performs a type check only at run time.


Using generally has the following uses:

1. Direct introduction of namespaces

A, using System, this is the most commonly used, is the using+ namespace, so you can directly use the type in the namespace, but eliminates the use of detailed namespaces

b, using fully qualified names
Do not use using System; Call System.Console.WriteLine ("Hello C #") directly in the program;

The first method is a more common method, which makes it easy to import the entire namespace to the current namespace at a time.
However, it is appropriate to use only one class in the namespace in the current namespace, such as a fully qualified name in the previous example.

In some cases, however, you must use a fully qualified name, such as a class with the same name in more than two namespaces that are introduced, such as a console class under System, and a console class with the same names in another custom namespace MyNamespace. Then if we introduce system and MyNamespace in the third namespace, then if we want to use a specific Console, we need the right to qualify the name System.Console or Mynamespace.console, otherwise the compiler does not know which Console we use specifically, the compilation cannot pass.

2.using alias. A using + alias = A specific type that includes detailed namespace information.
For example, we introduce the System.IO.Compression namespace with the following statement:
Using Zip=system.io.compression;
Then we can use a zip to represent the System.IO.Compression namespace, using Zip.gzipstream is to use System.IO.Compression.GZipStream. It is convenient to write the program.

One advantage of this approach is that when the same CS references two different namespaces, the two namespaces include a type with the same name. When this type is needed, it is necessary to use a detailed namespace approach to distinguish the types of these same names everywhere. The method of using aliases will be more concise, which class you use to make an alias declaration on which class. Note: Not that two names are duplicated, one is aliased, the other does not need to use aliases, and if two are to be used, then two will need to use to define aliases.

For example:

#中可使用using关键字自动清除对象资源.

# MyObject.cs

Using System;

Namespace MyProjects

{

public class Myobject:idisposable

{

Public MyObject ()

{

}

public void Dispose ()

{

Dispose

Console.WriteLine ("Disposed");

// ...

}

}

}

# Class1.cs

Using System;

Namespace MyProjects

{

public class Class1

{

Public Class1 ()

{

}

public static void Main (string[] args)

{

using (MyObject MyObject = new MyObject ())

{

Console.WriteLine ("Quit");

}

}

}

}

The MyObject Dispose method is invoked automatically with the use using.

If MyObject does not implement the IDispose interface, an error occurs at compile time: The type cannot be myprojects. MyObject "implicitly converted to" System.IDisposable "

This is an alternative to

Try

{ //... }

Catch

{ }

Finnaly

{Myobject.dispose ();}

3.using statement that defines a range that handles objects at the end of the range.

Scene:

When you use an instance of a class in a code snippet, you want to automatically call dispose of the class instance as long as you leave the code snippet for whatever reason. To achieve this, it is also possible to use Try...catch http://www.111cn.net/n78/to catch exceptions, but it is also convenient to use using the.

For example:

using (Class1 cls1 = new Class1 (), cls2 = new Class1 ())
{
The code using CLS1, CLS2
}

Call to Dispose on CLS1 and cls2 the Dispose condition that triggers cls1 and cls2 here is to reach the end of the using statement or to throw an exception and control to leave the statement block

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.