Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[code style] Prefer file-scope namespaces #28040

Conversation

MartyIX
Copy link
Contributor

@MartyIX MartyIX commented Feb 25, 2025

Description of Change

This PR adds a suggestion to use file-scoped namespaces (if possible) using IDE0161 analyzer.

The documentation page mentions:

This option [i.e. csharp_style_namespace_declarations] is used by Visual Studio to determine how namespaces are declared when new code files are added to projects. Visual Studio honors the option value even if both IDE0160 and IDE0161 are disabled.

This PR was created because when file-scoped namespaces are used, then there is bigger chance that two files can be worked on one display which leads to improved productivity.

What are file-scoped namespaces?

C# originally allowed only block-scoped namespaces:

using System;

namespace Microsoft.Maui
{
	internal static class ArrayExtensions
	{
	}
}

Modern C# versions support:

using System;

namespace Microsoft.Maui;

internal static class ArrayExtensions
{
}

as it saves one level of indentation, so more code fits on your screen.

@MartyIX MartyIX requested a review from a team as a code owner February 25, 2025 16:24
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Feb 25, 2025
Copy link
Contributor

Hey there @MartyIX! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@@ -67,6 +67,10 @@ csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = false:none

# Namespace preference (prefer file-scoped namespaces)
csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = suggestion
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion to avoid waaaay too many warnings.

@@ -67,6 +67,10 @@ csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = false:none

# Namespace preference (prefer file-scoped namespaces)
csharp_style_namespace_declarations = file_scoped
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Picked up by MSVS when user creates a new type (class, interface, etc.).

@jfversluis jfversluis added t/housekeeping ♻︎ area-architecture Issues with code structure, SDK structure, implementation details labels Feb 26, 2025
@jfversluis jfversluis added this to the .NET 9 SR5 milestone Feb 26, 2025
@jsuarezruiz
Copy link
Contributor

/azp run

1 similar comment
@PureWeen
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@rmarinho rmarinho merged commit 7a197f1 into dotnet:main Feb 27, 2025
49 of 52 checks passed
@MartyIX MartyIX deleted the feature/2025-02-25-editorconfig-file-scoped-namespaces branch February 27, 2025 15:38
tj-devel709 pushed a commit that referenced this pull request Mar 3, 2025
bhavanesh2001 pushed a commit to bhavanesh2001/maui that referenced this pull request Mar 7, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Mar 30, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-architecture Issues with code structure, SDK structure, implementation details community ✨ Community Contribution t/housekeeping ♻︎
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants