命名空間宣告喜好設定 (IDE0160 和 IDE0161)

本文描述兩個相關規則 IDE0160IDE0161

屬性
規則識別碼 IDE0160
標題 使用區塊範圍命名空間
類別 樣式
子類別 語言規則 (程式碼區塊喜好設定)
適用語言 C#
選項 csharp_style_namespace_declarations
屬性
規則識別碼 IDE0161
標題 使用檔案範圍命名空間
類別 樣式
子類別 語言規則 (程式碼區塊喜好設定)
適用語言 C#
選項 csharp_style_namespace_declarations

概觀

這些規則適用於命名空間宣告。 若要讓 IDE0161 在使用區塊範圍命名空間時報告違規,您必須將相關選項設定為 file_scoped

選項

選項值會指定命名空間宣告應為區塊範圍或檔案範圍。 根據預設,命名空間宣告為區塊範圍。 Visual Studio 會使用此選項來決定將新的程式碼檔案加入專案時要如何宣告命名空間。 即使同時停用 IDE0160IDE0161,Visual Studio 仍會接受選項值。

如需設定選項的資訊,請參閱選項格式

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

如需詳細資訊,請參閱如何隱藏程式碼分析警告

另請參閱