The best position of the instruction: Is it inside the naming space or the name space?
using
When writing the C# code, the placement of instructions is often confusing, especially when Stylecop suggests to put them in the name space. Although this seems to be randomly stipulated, there are slight differences between the two methods.
Let's take a look at an example.
using
file, the content is as follows: File1.cs
using System; namespace Outer.Inner { class Foo { static void Bar() { double d = Math.PI; } } }
named space instead of the external File2.cs
instructions, causing a reference to
namespace Outer { class Math { } }
However, if we put the Outer
instruction in the name space statement, as shown below: using
System.Math
naming space, and then search for using
naming space to correctly analyze the reference of
namespace Outer.Inner { using System; class Foo { static void Bar() { double d = Math.PI; } } }
It is worth noting that this behavior is also applicable to nested naming space, and the compiler always searches the innermost layer of naming space. For example, if is located in System
, then adding Outer
in the System.Math
in the
compilation failure. Foo
namespace Outer
Therefore, in order to avoid naming conflicts and ensure the correctness of the code, it is recommended to put the File2
instruction in the name space. Outer.Math
The above is the detailed content of Should C# `using` Directives Be Inside or Outside Namespaces?. For more information, please follow other related articles on the PHP Chinese website!