Does the location of the `using` directive make a difference in C#?

c#, syntax

Solution

I used to think that it did not matter, but I always fully qualify using commands.

Edit: Checked the C# spec, section 9.4 says that:

The scope of a using-directive specifically does not include its peer using-directives. Thus, peer using-directives do not affect each other, and the order in which they are written is insignificant.

So if System.Collections.Generic and KeyValuePair are treated as peers then KeyValuePair is going to ignore System.Collections.Generic.

Problem

I got an error today while trying to do some formatting to existing code. Originally, the code had the `using` directives declared outside the namespace: ``` using System.Collections.Generic; namespace MyNamespace { using IntPair = KeyValuePair<int, int>; } ``` When I tried to insert the `using` directive inside the statement (to comply with StyleCop's rules), I got an error at the aliasing directive, and I had to fully qualify it: ``` namespace MyNamespace { using System.Collections.Generic; //using IntPair = KeyValuePair<int, int>; // Error! using IntPair = System.Collections.Generic.KeyValuePair<int, int>; // works } ``` I wonder what difference there is between the two cases? Does the location of the (import-style) `using` directive matter?

Original source

Related problems