I person been moving StyleCop complete any C# codification, and it retains reporting that my using
directives ought to beryllium wrong the namespace.
Is location a method ground for placing the using
directives wrong alternatively of extracurricular the namespace?
Location is really a (refined) quality betwixt the 2. Ideate you person the pursuing codification successful File1.cs:
// File1.csusing System;namespace Outer.Inner{ class Foo { static void Bar() { double d = Math.PI; } }}
Present ideate that person provides different record (File2.cs) to the task that seems similar this:
// File2.csnamespace Outer{ class Math { }}
The compiler searches Outer
earlier trying astatine these using
directives extracurricular the namespace, truthful it finds Outer.Math
alternatively of System.Math
. Unluckily (oregon possibly fortuitously?), Outer.Math
has nary PI
associate, truthful File1 is present breached.
This modifications if you option the using
wrong your namespace declaration, arsenic follows:
// File1b.csnamespace Outer.Inner{ using System; class Foo { static void Bar() { double d = Math.PI; } }}
Present the compiler searches System
earlier looking out Outer
, finds System.Math
, and each is fine.
Any would reason that Math
mightiness beryllium a atrocious sanction for a person-outlined people, since location's already 1 successful System
; the component present is conscionable that location is a quality, and it impacts the maintainability of your codification.
It's besides absorbing to line what occurs if Foo
is successful namespace Outer
, instead than Outer.Inner
. Successful that lawsuit, including Outer.Math
successful File2 breaks File1 careless of wherever the using
goes. This implies that the compiler searches the innermost enclosing namespace earlier it seems astatine immoderate using
directive.
This thread already has any large solutions, however I awareness I tin carry a small much item with this further reply.
Archetypal, retrieve that a namespace declaration with durations, similar:
namespace MyCorp.TheProduct.SomeModule.Utilities{ ...}
is wholly equal to:
namespace MyCorp{ namespace TheProduct { namespace SomeModule { namespace Utilities { ... } } }}
If you needed to, you may option using
directives connected each of these ranges. (Of class, we privation to person using
s successful lone 1 spot, however it would beryllium ineligible in accordance to the communication.)
The regulation for resolving which kind is implied, tin beryllium loosely acknowledged similar this: Archetypal hunt the interior-about "range" for a lucifer, if thing is recovered location spell retired 1 flat to the adjacent range and hunt location, and truthful connected, till a lucifer is recovered. If astatine any flat much than 1 lucifer is recovered, if 1 of the sorts are from the actual meeting, choice that 1 and content a compiler informing. Other, springiness ahead (compile-clip mistake).
Present, fto's beryllium express astir what this means successful a factual illustration with the 2 great conventions.
(1) With usings extracurricular:
using System;using System.Collections.Generic;using System.Linq;//using MyCorp.TheProduct; <-- uncommenting this would change nothingusing MyCorp.TheProduct.OtherModule;using MyCorp.TheProduct.OtherModule.Integration;using ThirdParty;namespace MyCorp.TheProduct.SomeModule.Utilities{ class C { Ambiguous a; }}
Successful the supra lawsuit, to discovery retired what kind Ambiguous
is, the hunt goes successful this command:
- Nested sorts wrong
C
(together with inherited nested sorts) - Sorts successful the actual namespace
MyCorp.TheProduct.SomeModule.Utilities
- Sorts successful namespace
MyCorp.TheProduct.SomeModule
- Sorts successful
MyCorp.TheProduct
- Sorts successful
MyCorp
- Sorts successful the null namespace (the planetary namespace)
- Sorts successful
System
,System.Collections.Generic
,System.Linq
,MyCorp.TheProduct.OtherModule
,MyCorp.TheProduct.OtherModule.Integration
, andThirdParty
The another normal:
(2) With usings wrong:
namespace MyCorp.TheProduct.SomeModule.Utilities{ using System; using System.Collections.Generic; using System.Linq; using MyCorp.TheProduct; // MyCorp can be left out; this using is NOT redundant using MyCorp.TheProduct.OtherModule; // MyCorp.TheProduct can be left out using MyCorp.TheProduct.OtherModule.Integration; // MyCorp.TheProduct can be left out using ThirdParty; class C { Ambiguous a; }}
Present, hunt for the kind Ambiguous
goes successful this command:
- Nested sorts wrong
C
(together with inherited nested sorts) - Sorts successful the actual namespace
MyCorp.TheProduct.SomeModule.Utilities
- Sorts successful
System
,System.Collections.Generic
,System.Linq
,MyCorp.TheProduct
,MyCorp.TheProduct.OtherModule
,MyCorp.TheProduct.OtherModule.Integration
, andThirdParty
- Sorts successful namespace
MyCorp.TheProduct.SomeModule
- Sorts successful
MyCorp
- Sorts successful the null namespace (the planetary namespace)
(Line that MyCorp.TheProduct
was a portion of "Three." and was so not wanted betwixt "Four." and "5.".)
Concluding remarks
Nary substance if you option the usings wrong oregon extracurricular the namespace declaration, location's ever the expectation that person future provides a fresh kind with similar sanction to 1 of the namespaces which person greater precedence.
Besides, if a nested namespace has the aforesaid sanction arsenic a kind, it tin origin issues.
It is ever unsafe to decision the usings from 1 determination to different due to the fact that the hunt hierarchy adjustments, and different kind whitethorn beryllium recovered. So, take 1 normal and implement to it, truthful that you received't person to always decision usings.
Ocular Workplace's templates, by default, option the usings extracurricular of the namespace (for illustration if you brand VS make a fresh people successful a fresh record).
1 (small) vantage of having usings extracurricular is that you tin past make the most of the utilizing directives for a planetary property, for illustration [assembly: ComVisible(false)]
alternatively of [assembly: System.Runtime.InteropServices.ComVisible(false)]
.
Summation impressed by another thread: Say successful the supra illustration that the namespace MyCorp.TheProduct.System
occurred to be, equal although we person nary usage for it successful our record. Past with usings extracurricular, it would not alteration thing. However with usings wrong, you would person to usage the global
alias, similar this:
namespace MyCorp.TheProduct.SomeModule.Utilities{ using global::System; // avoids 'MyCorp.TheProduct.System' which also exists using global::System.Collections.Generic; // etc.}
Replace astir record-scoped namespace declarations
Since C# 10.Zero (from 2021), you tin debar indentation and usage both (normal 1, usings extracurricular):
using System;using System.Collections.Generic;using System.Linq;using MyCorp.TheProduct.OtherModule;using MyCorp.TheProduct.OtherModule.Integration;using ThirdParty;namespace MyCorp.TheProduct.SomeModule.Utilities;class C{ Ambiguous a;}
oregon (normal 2, usings wrong):
namespace MyCorp.TheProduct.SomeModule.Utilities;using System;using System.Collections.Generic;using System.Linq;using MyCorp.TheProduct;using MyCorp.TheProduct.OtherModule;using MyCorp.TheProduct.OtherModule.Integration;using ThirdParty;class C{ Ambiguous a;}
However the aforesaid concerns arsenic earlier use.
Mistake producing weblog contented