-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
SDK support for implicit namespaces in C# projects #25066
Comments
It might be helpful to show one potential error you might get, specifically if you're authoring an MSBuild task, and thus deriving from the Task abstract class, you will see errors like this because it conflicts with the Task class in System.Threading.Tasks, which is now being implicitly included:
If you get the above, you will need to disable this feature for the project. |
cc @gewarren |
So was there no discussion around this change? This change feels questionable to me, especially with the default lists. How did you arrive at these defaults? |
Arbitrary, sloppy implicit namespace polution can really use a warning that goes something like: Boilerplate that the project does for you is still boilerplate by the way, in case your college book didn't teach you that. Namespaces and using instructions are important enough that they should remain explicit and "boilerplate" for the sake of good etiquette. This change can have negative ramification for new developers in the same way that implicit |
SDK support for implicit namespaces in C# projects
We are introducing implicit namespace support for C# projects in the .NET SDK. The goal is to reduce the amount of boilerplate in .NET C# project templates by removing the need for
using
statements at the top of most files with the global using feature introduced in C# 10.Implicit namespace will be enabled by default for C# projects targeting the
net6.0
TFM or higher and usingMicrosoft.NET.Sdk
,Microsoft.NET.Sdk.Web
orMicrosoft.NET.Sdk.Worker
SDK. A generated file containing the default namespaces will be included in the set of files passed to the compiler. Projects using a C# version less than 10 need to explicitly disable this feature.Modifying the list of implicit namespaces
There are three ways to control the namespaces that are included in the project:
<DisableImplicitNamespaceImports>
totrue
in the project file.<Import>
item group in the project file. For exampleThese are the implicit namespaces added to projects using the following SDKs:
Microsoft.NET.Sdk
The following list of namespaces will be added:
This set of implicit namespace imports can be disabled by setting
<DisableImplicitNamespaceImports_DotNet>
totrue
in the project file.Microsoft.NET.Sdk.Web
The following list of namespaces will be added in addition to the ones added by the Microsoft.NET.Sdk:
This set of implicit namespace imports can be disabled by setting
<DisableImplicitNamespaceImports_Web>
totrue
in the project file.Microsoft.NET.Sdk.Worker
The following list of namespaces will be added in addition to the ones added by the Microsoft.NET.Sdk:
This set of implicit namespace imports can be disabled by setting
<DisableImplicitNamespaceImports_Worker>
totrue
in the project file.Version introduced
6.0-preview7
Old behavior
No implicit namespaces are added for applicable C# projects.
New behavior
Implicit namespaces are added via global usings in a generated file in the obj directory. See how to control the list of included implicit namespaces above.
Category
There is a potential need to resolve type conflicts due to the introduction of implicit namespaces.
Reason for change
These changes in the SDK are made to reduce the amount of boilerplate
using
statements that appear at the top of most C# projects.Recommended action
This feature will be enabled by default and for most users, no action will need to be taken. However, the introduction of this mechanism can cause type name conflicts with the namespaces listed above. In those cases, action need to be take to modify the list of implicit namespaces
Feature area
Affected APIs
Finally, please remember to email a link to this breaking change issue to .NET Breaking Change Notifications.
Issue metadata
The text was updated successfully, but these errors were encountered: