C#中using关键字的使用
NO1:
Provides a convenient syntax that ensures the correct use of IDisposable objects.
Example:
The following example shows how to use the using statement.
Remarks
File and Font are examples of managed types that access unmanaged resources (in this case file handles and device contexts). There are many other kinds of unmanaged resources and class library types that encapsulate them. All such types must implement the IDisposable interface.
When the lifetime of an IDisposable object is limited to a single method, you should declare and instantiate it in the using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.
The using statement ensures that Dispose is called even if an exception occurs within the using block. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. The code example earlier expands to the following code at compile time (note the extra curly braces to create the limited scope for the object):
{var font1 = new Font("Arial", 10.0f);try{byte charset = font1.GdiCharSet;}finally{if (font1 != null)((IDisposable)font1).Dispose();} }For more information about the try-finally statement, see the try-finally topic.
Multiple instances of a type can be declared in the using statement, as shown in the following example:
using (var font3 = new Font("Arial", 10.0f),font4 = new Font("Arial", 10.0f)) {// Use font3 and font4. }You can instantiate the resource object and then pass the variable to the using statement, but this is not a best practice. In this case, after control leaves the using block, the object remains in scope but probably has no access to its unmanaged resources. In other words, it’s not fully initialized anymore. If you try to use the object outside the using block, you risk causing an exception to be thrown. For this reason, it’s generally better to instantiate the object in the using statement and limit its scope to the using block.
var font2 = new Font("Arial", 10.0f); using (font2) // not recommended {// use font2 } // font2 is still in scope // but the method call throws an exception float f = font2.GetHeight();NO2:
The using directive has three uses:
A.To allow the use of types in a namespace so that you do not have to qualify the use of a type in that namespace:
B.To allow you to access static members and nested types of a type without having to qualify the access with the type name.
using static System.Math;C.To create an alias for a namespace or a type. This is called a using alias directive.
using Project = PC.MyCompany.Project;NO3:
The using static directive designates a type whose static members and nested types you can access without specifying a type name. Its syntax is:
where fully-qualified-type-name is the name of the type whose static members and nested types can be referenced without specifying a type name. If you do not provide a fully qualified type name (the full namespace name along with the type name), C# generates compiler error CS0246: “The type or namespace name ‘type/namespace’ could not be found (are you missing a using directive or an assembly reference?)”.
The using static directive applies to any type that has static members (or nested types), even if it also has instance members. However, instance members can only be invoked through the type instance.
Example:
The following example uses the using static directive to make the static members of the Console, Math, and String classes available without having to specify their type name.
In the example, the using static directive could also have been applied to the Double type. This would have made it possible to call the TryParse(String, Double) method without specifying a type name. However, this creates less readable code, since it becomes necessary to check the using static statements to determine which numeric type’s TryParse method is called.
? ? 上面內(nèi)容就是API文檔對(duì)using關(guān)鍵字的使用詳細(xì)解釋。查看文檔后,驗(yàn)證了網(wǎng)友與官網(wǎng)的一致性。關(guān)于上面內(nèi)容的中文理解,網(wǎng)上都是可以查到的喲。總結(jié)
以上是生活随笔為你收集整理的C#中using关键字的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【SQL】存储过程procedure
- 下一篇: c# char unsigned_dll