One thing I do not get with C#'s using
Directive is why I can only "use" a namespace at file level, and not within any arbitrary block scope.
(using namespace x;
of C++ does allow this and certainly the C# designers where aware of this.)
If I have, e.g., one single function in my class that does something with file I/O, it seems to me it would make sense to just write:
void MunchFile(string name) {
using System.IO; // not allowed
...
}
but instead I have to write:
// somewhere at the top of the file:
using System.IO;
... 2 pages down ...
void MunchFile(string name) {
...
}
It's an irritation every time I encounter it and I keep scratching my head why this was not allowed / implemented.
So:
- Are there any statements by C# designers wrt. this?
- Is there any conceptual language design issue wrt. this?
Best Answer
While he doesn't refer specifically to the using directive, Eric Gunnerson's article Minus 100 Points addresses the general question of "why doesn't C# have [feature X] which C++ has". The most relevant part reads:
He goes on to describe how the importance of a feature can be weighed against the complexity it introduces. Long story short, a lot of features that other languages have didn't make the cut, because the results they achieve simply aren't compelling enough for a language whose designers were aiming at simplicity.