Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting confusing "ambiguous reference" error

(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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.Sdk
  • Microsoft.NET.Sdk.Web
  • Microsoft.NET.Sdk.Worker
  • Microsoft.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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading