C#
Instruction: Calling the space or outside?
using
When checking the C# code with Stylecop, you may receive a warning, and it is recommended that the instruction statement in the name space. Although some developers may be more inclined to put them outside the name space, there is indeed a technical reason to consider putting them in the name space.
The influence of file organization using
Imagine the two files, file1.cs and file2.cs, as shown below: File1.cs:
File2.cs:
<code class="language-csharp">using System; namespace Outer.Inner { class Foo { static void Bar() { double d = Math.PI; } } }</code>
Solution: Place instructions in the name space
<code class="language-csharp">namespace Outer { class Math { } }</code>
Placing in the name space (as shown in the following File1b.cs) can ensure that searching System before searching for internal name space: using
In this case, the compiler will find System.math to solve the potential errors caused by the definition of conflict in different naming spaces.
using
Precautions
instructions can be placed in the naming space to simplify code maintenance by ensuring consistent reference space. It can also highlight the potential conflict between classes defined and System named spaces defined by the user. using
<code class="language-csharp">namespace Outer.Inner { using System; class Foo { static void Bar() { double d = Math.PI; } } }</code>
instruction is placed.
The above is the detailed content of Should C# `using` Directives Be Placed Inside or Outside the Namespace?. For more information, please follow other related articles on the PHP Chinese website!