(Disclaimer): I know what an ambiguous reference is and how to solve it. I am just confused why this appears here
I have the following code:
using SMBLibrary;
namespace SquirrelCam
{
public class NothingAtAll
{
public void DoNothing()
{
var fileAttribute = FileAttributes.Normal;
}
}
}
that creates the following error in VS:
CS0104: ‘FileAttributes’ is an ambiguous reference between ‘SMBLibrary.FileAttributes’ and ‘System.IO.FileAttributes’
I know I can solve this by fully qualify to SMBLibrary.FileAttributes. I just wonder why this appears. I have no usings to System.IO anywhere. Not in the class, not globally. I even did a fulltext – search just to be sure.
I also only have to nuget packages (SMBLibrary and MS.AspNet.WebHooks.Receivers.Generic) which also do not seem to have dependencies on System.IO according to the nuget description. Also nothing to be found in the .csproj – file.
The project has unsurprisingly dependencies on NetCore.App which uses System.IO, but that alone should not be "enough" to cause that behaviour.
I already cleaned up the solution and still get that warning.
>Solution :
Starting .NET 6 default templates enable implicit global usings:
Starting in .NET 6, implicit global using directives are added to new C# projects. This means that you can use types defined in these namespaces without having to specify their fully qualified name or manually add a using directive. The implicit aspect refers to the fact that the global using directives are added to a generated file in the project’s obj directory.
Implicit global using directives are added for projects that use one of the following SDKs:
Microsoft.NET.SdkMicrosoft.NET.Sdk.WebMicrosoft.NET.Sdk.WorkerMicrosoft.NET.Sdk.WindowsDesktop
And System.IO is one of the implicitly imported namespaces (check out the already linked docs) hence the problem when you are adding using SMBLibrary; which also has FileAttributes.
You can completely disable the feature by setting ImplicitUsings to disable in the csproj file:
<ImplicitUsings>disable</ImplicitUsings>
Or just removing the not needed ones by adding Using with Remove attribute:
<ItemGroup>
<Using Remove="System.IO"/> <!-- will remove only the System.IO-->
</ItemGroup>