C# using 的使用方法


1.  using :對命名空間的引用

      比如 using System; 這樣的命名空間,在加入了dll 包之后,也要對包進行引用

     對不同命名空間同一方法別名的區分即:定義別名

using System;


namespace
someName1 { public class some { public string getSomeString() { return "this is method of someName1"; } } } namespace someName2 { public class some { public string getSomeString() { return "this is method of someName2"; } } }

 定義兩個命名空間

 2. using:定義別名

using oneName = someName1.some;
using twoName = someName2.some;

 

下面是使用      

oneName one = new oneName();
Console.WriteLine( one.getSomeString());
twoName two = new twoName();
Console.WriteLine(two.getSomeString());
Console.Read();

作用:這樣就避免了很多重名的麻煩,而且,使得有些很長的命名空間的名字的以簡化

 

3. using:自動釋放所新建的對象;

作用:① 自動釋放,避免緩存,內存溢出

           ②  簡化try catch 得到在此定義域內自動釋放所新建的對象,以簡化代碼;

using (Class1 cls1 = new Class1(), cls2 = new Class1())
{
// the code using cls1, cls2
} // call the Dispose on cls1 and cls2

或
//自動釋放所新建的二維碼對象 using (MemoryStream ms = new MemoryStream()) { qrCodeImage.Save(ms, ImageFormat.Jpeg); returnImageData = ms.GetBuffer(); ms.Close(); }

 

 

參考文章:https://blog.csdn.net/echoerror/article/details/80907738 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM