3

DUPE: Should 'using' statements be inside or outside the namespace?

If I add a new class using Visual Studio to the project, It adds all the using statements before namespace but for the same project, FxCop says that put the name spaces inside namespace. Which is a better style of coding?

Community
  • 1
  • 1
Kapil
  • 9,469
  • 10
  • 40
  • 53

5 Answers5

4

See Should 'using' statements be inside or outside the namespace?

Community
  • 1
  • 1
Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
1

I prefer the default which is at the top of the file. This is completely up to you but I guess with the majority of people using the default template of Visual Studio and having them at the top, it'll be expected there.

With the namespaces outside of your namespace, it does become more readable as, to me, the namespaces are not as important as the code that you are writing.

Ray Booysen
  • 28,894
  • 13
  • 84
  • 111
0

If you put the Using inside the namespace, they apply to that namespace only.
If they are outside, they will apply to any namespace in the file

0

This is up to your personal taste. FxCop is based on the Microsoft standards. But to behonest I prefer the Visual Studio Default way, of being outside of the namespace.

Nick Berardi
  • 54,393
  • 15
  • 113
  • 135
0

Using directives bring names into scope. With that definition in mind, and also noting that you can have multiple namespaces in the same file, it makes sense for those names effected by a using directive to only apply within a specific namespace, since grouping names are what namespaces are for.

Nevermind that FxCop probably complains about multiple namespaces per file as well.

But in this case, I prefer the VS default. Why make extra work for myself?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794