Re: [問題] 請問一下using abc = Int32 是什麼意思?
using 有幾個用途
- using directive: for namespace
- using statement: for IDisposable
兩個都很有用
在業界軟體開發中 namespace通常會取非常長
當你的程式很複雜 你還會有一大堆DLL和一大堆namespace
所以在程式中的 using xxx.xxx.xxx.xxx 就會非常有用
取別名也是一個偶爾會用到的 using xxx = xxx.xxx.xxx.xxx
using statement如果你不常用到IDisposable可能就不知道
在GC的世界裡 你不需要release memory,
所以大部分的class不需要destructor(應該說finalizer)
但是不幸的有些是需要的 像是file handle, 或是unmanaged resource
finalizer是非常耗資源的 而且糟糕的是你無法預測什麼時候會finalize
所以一個common pattern是採用IDisposable 而且盡量在你結束使用的時候就Dispose
比如說:
try
{ ... }
finally
{ x.Dispose(); }
而using statement主要就是提供你一個簡單的 syntax來作這種事情
using (X x = new X()) { ... } 保證你的x object在離開{}的時候被Dispose
附帶一提Dispose通常要SuppressFinalize() 有興趣可以看看相關文章
※ 引述《EricTsai (I am the one...)》之銘言:
: ※ 引述《scdog (嘿ﰠNN  )》之銘言:
: : 請問一下常常看到有人寫程式用
: : using a = Int32;
: 給Int32取個別名叫a
: 以後Int32 i;就可以換成a i;,
: 但是個人覺得這個例子蠻沒意義的....
: using Money=Double;讓人看到Money就知道是存錢,實際上是用double存
: 或是using TcpC=System.Net.Sockets.TcpClient;給長長的TcpClient一個短一點的名字
: : 還有
: : public enum b{
: : c = 0x00;
: : d = 0x01;
: : }
: : 這是什麼意思??thx^^
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 131.107.71.93
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 5 之 5 篇):
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章