Putting the using statement inside the namespace block scopes it to that block. This affects a number of things.
- As @Steve Haigh mentioned, the
using statement is only valid within the block, so if there were other namespace blocks, they wouldn't be affected.
- The namespace specified by the
using can be shortened based on the outer namespace block. So using x.y; outside of the namespace can just be expressed as using y; inside of the namespace x block.
- Putting the
using inside of the namespace causes the compiler to guarantee that the specified namespace isn't overwritten. For example:
using Guid = System.Guid;
namespace Sample
{
public class Guid {}
public class Program
{
public static void Main()
{
Console.WriteLine(new Guid());
}
}
}
The above code will compile, but it is unclear which Guid is being instantiated. If the using statement is inside of the namespace block, however, a compiler error is thrown.
See the related StyleCop documentation for a more complete discussion.