排序 Using
Visual Studio 使用者介面的 [排序 Using] 選項可依字母順序組合管理 using 指示詞、using 別名及 extern 別名來改善原始程式碼的可讀性,排列順序如下:
extern 別名
using 指示詞
using 別名
注意事項 根據預設,Visual Studio 會將開頭為 System 的 using 指示詞排在其他 using 指示詞之前。您可以修改 [排序 Using],依字母順序來排序所有 using 指示詞。如需詳細資訊,請參閱進階、C#、文字編輯器、選項。
呼叫作業的方式有兩種:
主功能表:在 [編輯] 功能表上,指向 [IntelliSense] 及 [組合管理 Using],然後按一下 [排序 Using]。
內容功能表:以滑鼠右鍵按一下程式碼編輯器內的任何位置,指向 [組合管理 Using],然後按一下 [排序 Using]。
下列範例顯示在原始程式碼中執行 [排序 Using] 的結果。
之前 |
之後 |
---|---|
extern alias ApressLibrary2; extern alias ApressLibrary1; using aio = apressio; using System.Collections; using Microsoft.CSharp; using System; using apressio = Apress.IO; |
extern alias ApressLibrary1; extern alias ApressLibrary2; using System; using System.Collections; using Microsoft.CSharp; using aio = apressio; using apressio = Apress.IO; |
備註
前置處理器指示詞
以前置處理器指示詞分隔指示詞或別名時,[排序 Using] 將無法排序。例如,下列程式碼無法排序。
// Not sorted because preprocessor directives separate the using directives.
using System.Linq;
#region MyRegion
using System.Collections.Generic;
using System;
#endregion
using System.Collections;
不過,下列範例將可排序。
// Sorted because pre-processor directives do not separate using directives
#region MyRegion
using System.Collections;
using System;
using System.Collections.Generic;
#endregion
註解
在排序期間,會將指示詞上方或內嵌的註解歸類於該指示詞。下列範例可說明此行為。
之前 |
之後 |
---|---|
// © Contoso, Ltd using apressdata = Apress.Data; using aio = apressio; using System.Collections; using System; // using System; using System.Collections.Generic; // using System.Text using System.Text; using apressio = Apress.IO; // The End |
using System; // using System; using System.Collections; using System.Collections.Generic; // using System.Text using System.Text; using aio = apressio; // © Contoso, Ltd using apressdata = Apress.Data; using apressio = Apress.IO; // The End |
在上述範例中,// © Contoso, Ltd 註解會以它下方的 using 指示詞來分組和排序,因為指示詞與註解之間沒有歸位字元。若要避免這種行為,請在註解後加入額外的歸位字元。