Why is the C# using directive called a "directive" when it does not affect the compilation process?

StackOverflow https://stackoverflow.com/questions/22352981

  •  13-06-2023
  •  | 
  •  

Question

Question

Why is the C# using directive called a directive when it does not affect the compilation process?

Is there a difference between the terms directive and preprocessor directive (examples)?

This question is about terminology.

Elaboration

There is a using directive and there is a using statement. I'm talking about the using directive here.

Wikipedia says that directives are instructions for the compiler which change the compilation process.

This Stackoverflow answer says that the C# using directive does not change the compilation process in any way.

So, if the using directive doesn't change the compilation process, why is it still called a directive?

Was it helpful?

Solution

The answer you link to states that removing unused using directives doesn't affect compilation output.

The using directives list the namespaces the compiler is allowed to implicitly reference in the generated code. Without the directive, you must fully qualify all names, so:

using System.Diagnostics;
...
var x = new Process();

instead of

var x = new System.Diagnostics.Process();

So it is a true directive in the sense that it changes how the compiler interprets your code.

A Preprocessor Directive is applied to change the source code before it is compiled. Preprocessor Directives in C# are very limited. Unlike in C / C++ -- where you can entirely re-write the source code before it is compiled -- in C# you can turn blocks of code on and off, and change a few compiler flags. As far as I'm aware, they aren't applied in a separate stage as in C / C++.

And for completeness, the using statement is syntactic sugar, turning

using (var x = new IDisposable()) {}

(where var x = is optional)

into

var x = new IDisposable();
try{ ... } 
finally { x.Dispose(); }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top