我一直在試圖瞭解更多的C#語言,但我一直沒能看到的局面,其中一個會使用命名空間別名像C#名稱空間別名 - 有什麼意義?
using someOtherName = System.Timers.Timer;
在我看來,它只會增加更多的困惑瞭解語言。 有人可以解釋一下。
由於
我一直在試圖瞭解更多的C#語言,但我一直沒能看到的局面,其中一個會使用命名空間別名像C#名稱空間別名 - 有什麼意義?
using someOtherName = System.Timers.Timer;
在我看來,它只會增加更多的困惑瞭解語言。 有人可以解釋一下。
由於
即一個類型別名,而不是一個命名空間別名;它是有用的歧義 - 例如,反對:
using WinformTimer = System.Windows.Forms.Timer;
using ThreadingTimer = System.Threading.Timer;
(PS:感謝Timer
;-p的選擇)
否則,如果在同一文件中同時使用System.Windows.Forms.Timer
和System.Timers.Timer
」 d必須繼續給出全名(因爲Timer
可能會令人困惑)。
它也與extern
別名一起使用,它們使用來自不同程序集的具有相同完全限定類型名稱的類型 - 很少見,但對於支持很有用。
其實,我可以看到另一種用法:當你想快速訪問類型,但不希望使用正using
,因爲你不能導入一些相互矛盾的擴展方法......一個有點令人費解,但是...這裏是一個例子...
namespace RealCode {
//using Foo; // can't use this - it breaks DoSomething
using Handy = Foo.Handy;
using Bar;
static class Program {
static void Main() {
Handy h = new Handy(); // prove available
string test = "abc";
test.DoSomething(); // prove available
}
}
}
namespace Foo {
static class TypeOne {
public static void DoSomething(this string value) { }
}
class Handy {}
}
namespace Bar {
static class TypeTwo {
public static void DoSomething(this string value) { }
}
}
當在多個包含的命名空間中有多個具有相同名稱的類時,這非常有用。例如...
namespace Something.From.SomeCompanyA {
public class Foo {
/* ... */
}
}
namespace CompanyB.Makes.ThisOne {
public class Foo {
/* ... */
}
}
可以使用別名使編譯器高興,把事情對你和你的團隊其他更加清晰:
using CompanyA = Something.From.CompanyA;
using CompanyB = CompanyB.Makes.ThisOne;
/* ... */
CompanyA.Foo f = new CompanyA.Foo();
CompanyB.Foo x = new CompanyB.Foo();
我一直用它,在這樣的情況下,
using Utility = MyBaseNamespace.MySubNamsepace.Utility;
其中Utility
否則將有不同的上下文(如MyBaseNamespace.MySubNamespace.MySubSubNamespace.Utility
),但我期待/喜歡Utility
始終指向一個p關節類。
簡潔。
在共享類型名稱的命名空間之間提供清晰的好處,但本質上它只是糖。
我使用它時,我有相互衝突的子命名空間和/或對象的名字,你可能只是做類似[作爲示例]多個命名空間:
using src = Namespace1.Subspace.DataAccessObjects;
using dst = Namespace2.Subspace.DataAccessObjects;
...
src.DataObject source = new src.DataObject();
dst.DataObject destination = new dst.DataObject();
,否則必須寫:
Namespace1.Subspace.DataAccessObjects.DataObject source =
new Namespace1.Subspace.DataAccessObjects.DataObject();
Namespace2.Subspace.DataAccessObjects.DataObject dstination =
new Namespace2.Subspace.DataAccessObjects.DataObject();
它節省了大量的打字量,可以用來使代碼更容易閱讀。
我們爲所有的命名空間定義了命名空間別名。這使得它很容易看到其中一類來自於,e.g:
using System.Web.WebControls;
// lots of other using statements
// contains the domain model for project X
using dom = Company.ProjectX.DomainModel;
// contains common web functionality
using web = Company.Web;
// etc.
和
// User from the domain model
dom.User user = new dom.User();
// Data transfer object
dto.User user = new dto.User();
// a global helper class
utl.SomeHelper.StaticMethod();
// a hyperlink with custom functionality
// (as opposed to System.Web.Controls.HyperLink)
web.HyperLink link = new web.HyperLink();
我們已經確定了一些準則別名必須如何命名,每個人都在使用它們。
除了實施例中提到,類型別名(而不是命名空間的別名),可以很方便的時候多次提到泛型類型:
Dictionary<string, SomeClassWithALongName> foo = new Dictionary<string, SomeClassWithALongName>();
private void DoStuff(Dictionary<string, SomeClassWithALongName> dict) {}
對戰:
using FooDict = Dictionary<string, SomeClassWithALongName>;
FooDict foo = new FooDict();
private void DoStuff(FooDict dict) {}
我找到別名很在單元測試中很有用。當你寫單元測試,它是一種常見的做法是申報主體,以測試爲
MyClass myClassUT;
是myClassUT
主題ü的nDer 牛逼 EST。但是如果你想要寫一個單元測試靜態類與靜態方法?然後,你可以這樣創建別名:
using MyStaticClassUT = Namespace.MyStaticClass;
然後你就可以編寫單元測試是這樣的:
public void Test()
{
var actual = MyStaticClassUT.Method();
var expected = ...
}
和你的視線被測對象是什麼永不鬆動。
從某種角度來看,在Visual Studio中進行編碼確實非常方便。
用例:假設我只使用了幾個類,例如來自命名空間System.Data
的SqlConnection
。在正常情況我會在* cs文件的頂部導入System.Data.SqlClient
命名空間,如下圖所示:
using System.Data;
現在看我的智能感知。當在代碼編輯器中輸入時,它有很多類可供選擇。我不會在所有使用一大堆類:
所以,我寧願使用別名在我的* cs文件的頂部,並得到一個明確的智能感知觀點:
using SqlDataCon = System.Data.SqlClient.SqlConnection
現在看看我的intellisense視圖。它超級清晰,超級乾淨。
我知道的一個原因;當您從導入的名稱空間導致名稱衝突時,它允許您使用較短的名稱。 例子:
如果宣佈在同一文件using System.Windows.Forms;
和using System.Windows.Input;
當你去訪問ModifierKeys
你可能會發現這個名字ModifierKeys
是在兩個System.Windows.Forms.Control
和System.Windows.Input
命名空間。 因此,通過聲明using Input = System.Windows.Input;
,您可以通過Input.ModifierKeys
獲得System.Windows.Input.ModifierKeys
。
我不是C#的buff,但別名名稱空間對我來說好像是「最佳實踐」。這樣你就知道你得到了什麼,而且不必輸入太多。
如何在C#中使用int = System.Int32`的系統?有用,不是嗎?它的用處與其他地方相同。 – nawfal 2013-05-04 06:47:58
@nawfal我相信類型別名不可導出。意思是你不能像`using int = System.Int32`那樣定義一些東西,並且在聲明文件以外的地方使用它。所以這個「int」到「Int32」別名可以通過其他方式來實現,或者在編譯器/運行時是一件特殊的事情。 – KFL 2016-10-19 23:01:27
@KFL的確如此,但這兩方面的好處都具有相同的性質。 – nawfal 2016-10-19 23:03:46