File-scoped namespace enabled by default

Answered

When creating a new C# class, I would like to have file-scoped namespaced class by default (for proper C# language).

Is that possible in Rider?

0
3 comments

Hello,

If the project C# language version is >=10.0, the class will be created with file-scoped namespace by default.
Please ensure the setting `File | Settings | Editor | Code Style | C# | Syntax Style | Code body | Namespaces` is set to File-scoped.

The language version is set to 10.0 by default in projects that target .NET6. You can force your project to use the exact language version by editing the .csproj or via `Right-click on project in Solution Explorer | Properties | Application | Language version`.

Sample .csproj with a language version set:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>
        <LangVersion>10</LangVersion>
    </PropertyGroup>
</Project>

Please let me know if it doesn't help or anything is unclear.
Have a nice day!

1

Thank you for the help, Dmitry.

I've just found I had a setting overwritten in .editorconfig:

csharp_style_namespace_declarations = block_scoped:silent

I removed that setting and now it works fine. Thank you!

1

Hello,

I am happy to hear that I was able to help you.

Should you have any other questions or difficulties in the future, do not hesitate to contact support.
Have a nice day!

1

Please sign in to leave a comment.