日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > C# >内容正文

C#

C#中using关键字的使用

發(fā)布時(shí)間:2023/12/20 C# 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#中using关键字的使用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? ? 前段時(shí)間在項(xiàng)目驗(yàn)收的過(guò)程中,驗(yàn)收人員提到了打開(kāi)數(shù)據(jù)庫(kù)使用完畢之后有沒(méi)有及時(shí)關(guān)閉,然后就打開(kāi)代碼看了一眼,沒(méi)有Close()方法呢。在檢查過(guò)程中發(fā)現(xiàn),雖然沒(méi)有直接使用Close()方法,但在執(zhí)行連接數(shù)據(jù)庫(kù)時(shí)使用了using關(guān)鍵字,然后驗(yàn)收人員便說(shuō)沒(méi)有問(wèn)題 。為什么沒(méi)有使用Close()方法,僅有using關(guān)鍵字也是可以的呢?于是上網(wǎng)查了相關(guān)的資料,發(fā)現(xiàn)大家說(shuō)的都是大同小異的,不清楚大家說(shuō)的是不是都有道理,還有正確性如何。有那么一瞬間想到了紀(jì)老師提到的API文檔,于是就想看看官方到底是如何解釋的,來(lái)驗(yàn)證一下網(wǎng)上說(shuō)的。下面是官方的解釋: The using keyword has three major uses: 三個(gè)主要的用法: NO1:The using statement defines a scope at the end of which an object will be disposed. NO2:The using directive creates an alias for a namespace or imports types defined in other namespaces. NO3:The using static directive imports the members of a single class.

NO1:
Provides a convenient syntax that ensures the correct use of IDisposable objects.
Example:
The following example shows how to use the using statement.

using (var font1 = new Font("Arial", 10.0f)) {byte charset = font1.GdiCharSet; }

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:

using System.Text;

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:

using static <fully-qualified-type-name>;

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.

using System; using static System.Console; using static System.Math; using static System.String;class Program {static void Main(){Write("Enter a circle's radius: ");var input = ReadLine();if (!IsNullOrEmpty(input) && double.TryParse(input, out var radius)) {var c = new Circle(radius);string s = "\nInformation about the circle:\n";s = s + Format(" Radius: {0:N2}\n", c.Radius);s = s + Format(" Diameter: {0:N2}\n", c.Diameter);s = s + Format(" Circumference: {0:N2}\n", c.Circumference);s = s + Format(" Area: {0:N2}\n", c.Area);WriteLine(s);}else {WriteLine("Invalid input...");}} }public class Circle {public Circle(double radius){Radius = radius;}public double Radius { get; set; }public double Diameter{get { return 2 * Radius; }}public double Circumference{get { return 2 * Radius * PI; } }public double Area{get { return PI * Pow(Radius, 2); }} } // The example displays the following output: // Enter a circle's radius: 12.45 // // Information about the circle: // Radius: 12.45 // Diameter: 24.90 // Circumference: 78.23 // Area: 486.95

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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。