Is there a setting in Visual Studio (or ReSharper) that allows you to specify what namespaces should be default and which scope they are put in?
The default for an MVC project for example is
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Namespace { public class Class1 { } }
but ReSharper and StyleCop complain:
All using directives must be placed inside of the namespace. [StyleCop Rule: SA1200]
Using directive is not required by the code and can be safely removed
Is there a way to make the default simply:
namespace Namespace { public class Class1 { } }
As a rule, external using directives (System and Microsoft namespaces for example) should be placed outside the namespace directive. They are defaults that should be applied in all cases unless otherwise specified.
If you are adding a new class in your code then you may need to add the correspondence namespaces. To do it, you may either manually type the Using Namespace or you just right click on the class name and select Resolve > Namespace. But using “Ctrl+.” you can automatically add the namespace in your code.
The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types. C# Copy.
You can set this in Re-sharper.
Re-sharper > Options > C# > Namespace Imports > Add using directive to the deepest scope.
Update: As of VS2015 and Resharper10, this has moved. It is now under:
Code Editing > C# > Code Style > Reference qualification > Add 'using' directive to deepest scope
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With