本文說明兩個相關規則,IDE0160 和 IDE0161。
| 財產 | 價值 |
|---|---|
| 規則標識碼 | IDE0160 |
| 標題 | 使用區塊範圍命名空間 |
| 類別 | 風格 |
| 子類別 | 語言規則 (程式代碼區塊喜好設定) |
| 適用的語言 | C# |
| 選項 | csharp_style_namespace_declarations |
| 財產 | 價值 |
|---|---|
| 規則標識碼 | IDE0161 |
| 標題 | 使用檔案範圍命名空間 |
| 類別 | 風格 |
| 子類別 | 語言規則 (程式代碼區塊喜好設定) |
| 適用的語言 | C# |
| 選項 | csharp_style_namespace_declarations |
概述
這些規則適用於 命名空間宣告。 若要在使用區塊範圍命名空間時由 IDE0161 報告違規,您必須將相關選項設定為 file_scoped。
選項
選項值會指定命名空間宣告應設為區塊範圍或檔案範圍。 根據預設,命名空間宣告是區塊範圍。 Visual Studio 會使用此選項來判斷將新的程式碼檔案新增至專案時,如何宣告命名空間。 即使停用 IDE0160 和 IDE0161,Visual Studio 仍會接受選項值。
如需設定選項的相關資訊,請參閱 選項格式。
C# 匿名空間宣告風格(csharp_style_namespace_declarations)
| 財產 | 價值 | 描述 |
|---|---|---|
| 選項名稱 | csharp_style_namespace_declarations | |
| 適用的語言 | C# | |
| 發布版本 | Visual Studio 2019 | |
| 選項值 | block_scoped |
命名空間宣告應設定區塊範圍。 |
file_scoped |
命名空間宣告應設定檔案範圍。 | |
| 預設選項值 | block_scoped |
// csharp_style_namespace_declarations = block_scoped
using System;
namespace Convention
{
class C
{
}
}
// csharp_style_namespace_declarations = file_scoped
using System;
namespace Convention;
class C
{
}
隱藏警告
如果您想要只隱藏單一違規,請將預處理器指示詞新增至原始程式檔以停用,然後重新啟用規則。
#pragma warning disable IDE0160 // Or IDE0161
// The code that's violating the rule is on this line.
#pragma warning restore IDE0160 // Or IDE0161
若要停用檔案、資料夾或項目的規則,請將其嚴重性設定為 組態檔中的 none。
[*.{cs,vb}]
dotnet_diagnostic.IDE0160.severity = none
dotnet_diagnostic.IDE0161.severity = none
若要停用所有程式碼樣式規則,請將類別 Style 的嚴重性設定為 組態檔中的 none。
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none
如需詳細資訊,請參閱 如何在隱藏程式代碼分析警告。