3

What causes the c# compiler to say "using directive is unnecessary" on a using directive?

Presumably current namespace. What else? Is this defined somewhere in the documentation? I couldn't find a definitive answer to a google search.

EDIT to clarify, I'm looking for a list of places where the compiler looks to determine that a directive is not needed.

E.g. current namespace project references

I'm not looking for a simple fix (e.g. remove it) I'm trying to understand why it happens. Pointing me to any documentation that explains it would be awesome.

andrea
  • 481
  • 2
  • 8
  • 27
  • 2
    This meanas you don't use any type form specified namespace. – BWA Jan 23 '18 at 13:07
  • 1
    Look here, not a duplicate, but a related question with answers that might explain a few things: https://stackoverflow.com/q/136278/1220550 – Peter B Jan 23 '18 at 13:09
  • [Here](https://social.msdn.microsoft.com/Forums/vstudio/en-US/f49272d2-7294-4df9-a1eb-2ec59d438e66/what-makes-a-particular-using-directive-unnecessary?forum=csharpgeneral) you have some discusion about it. – BWA Jan 23 '18 at 13:12
  • It just means that the current code file would compile even if the `using` directive is removed. That's all. The only place you need to look is in the current code file. – Matthew Watson Jan 23 '18 at 13:20
  • 1
    As noted, the warning simply means that the program would be exactly the same even if that line was removed. But *why* give the warning? Two reasons. First, a developer might be misled into believing a file has a dependency when it does not. Second, and more importantly, *if you believe that a directive is needed and it isn't then some of your bindings might be to types that you did not intend them to go to!* The warning is subtly calling out that *you might have a false belief about your program*, and encouraging you to examine that belief. – Eric Lippert Jan 23 '18 at 15:43

1 Answers1

6

"Using directive is unnecessary" means that nothing in the current C# code file is referencing a non-namespace qualified type in the namespace of the using directive. To fix this warning you either need to remove the using directive or add some code that uses a type in the namespace the using statement refers to.

I'm looking for a list of places where the compiler looks to determine that a directive is not needed.

Since namespaces are a assembly-specific thing that can be extended by any assembly, there is no such documented list as it would not have a practical use. However, there are a couple of ways you can find out what types would eliminate the warning message.

In Visual Studio

You can use the Object Browser to determine what types are in a namespace.

enter image description here

Alternatively, you can use Intellisense to get the same information in the code window.

enter image description here

In Code

It is possible to use Reflection to get a list of all types in that namespace in a specific assembly.

string nspace = "...";

var q = from t in typeof(SomeClassInTheAssembly).Assembly.GetTypes()
        where t.IsClass && t.Namespace == nspace
        select t;
q.ToList().ForEach(t => Console.WriteLine(t.Name));

Keeping in mind that a namespace can span several assemblies, this approach could be used to show possible types that could be added to remove the warning.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • 4
    Not quite. If you have `using System.Windows.Forms` and in the code you have `System.Windows.Forms.MessageBox.Show("Hello");` you will get the warning - even though something in the current C# code file IS referencing a type in the namespace of the using directive. You get the warning if it is not necessary for the compile to succeed (which is pretty much what the warning says!) – Matthew Watson Jan 23 '18 at 13:18
  • 1
    Thanks. I have improved my answer. – NightOwl888 Jan 23 '18 at 13:22