Reputation: 521
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.
Upvotes: 3
Views: 20874
Reputation: 56869
"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.
You can use the Object Browser to determine what types are in a namespace.
Alternatively, you can use Intellisense to get the same information in the code window.
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.
Upvotes: 7