Does anyone know why there is no standalone C# preprocessor? Maybe because of some syntactic features of C# that makes it harder to run a preprocessor phase separately?
-
What features/value would such a preprocessor provide? – Cyprien Autexier May 26 '15 at 22:48
-
You just run the preprocessor over a C# file and then get the preprocessed one, like the standard CPP. It will be useful for analysis of C# files, when you don't need the full compilation process. – Wickoo May 26 '15 at 22:51
-
there is something called t4 which is a templating engine. – Daniel A. White May 26 '15 at 22:53
-
What sort of analysis are you trying to perform. Perhaps there is a way to do it without using a preprosser? – Scott Chamberlain May 26 '15 at 22:55
-
@ScottChamberlain the problem is that without resolving conditional directives, a C# program may be syntactically invalid. For example, something like this: https://github.com/ckaestne/TypeChef which runs preprocessor on C files beforehand. – Wickoo May 26 '15 at 22:57
-
1because [all features are unimplemented by default](http://stackoverflow.com/a/8673015/643085). Why do you need such a thing? – Federico Berasategui May 26 '15 at 23:00
-
@Wickoo it is likely HighCore did not see your response before he wrote his, SO has the habit of caching pages. Now do you have access to Visual Studio 2015, if so what you want is easily doable. – Scott Chamberlain May 26 '15 at 23:02
-
@ScottChamberlain These comments get very messy indeed. Which feature of VS 15 will help? The t4 template engine that another user pointed out? – Wickoo May 26 '15 at 23:03
-
t4 is not the feature I was referring to, I was referring to [Roslyn](https://github.com/dotnet/roslyn) (the complier that ships with Visual Studio 2015), specifically its [Syntax Analysis features](https://github.com/dotnet/roslyn/blob/master/docs/samples/csharp-syntax.pdf) – Scott Chamberlain May 26 '15 at 23:06
-
@ScottChamberlain Thanks! so now there is a way to access ASTs. – Wickoo May 26 '15 at 23:08
-
Yes, but you need VS 2015 to do it. – Scott Chamberlain May 26 '15 at 23:08
2 Answers
Because there has never really been a preprocessor, and some functionalities that are exposed via the #
prefixed lines interact with other parts of the compiler.
That #define SOME_VARIABLE
, #if
look like C/C++ preprocessor directives is nothing more than a syntactic choice by the designers of C#.
In particular, these "directives" are not orthogonal to the rest of the compilation process, for example the ConditionalAttribute
will inspect the build variables in order to include or exclude a given call when compiling or not.

- 16,518
- 5
- 56
- 90
-
1Actually, the C# `#define` don't even act like their C++ counterparts - they can only "introduce" a variable, but not set a value to it. – matan129 May 26 '15 at 23:05
The C# reference says:
Although the compiler does not have a separate preprocessor, the directives described in this section are processed as if there were one.
From this I infer that it is feasible (from a linguistic perspective) to separate out the preprocessor into a separate tool. But Microsoft haven't done this.
I can think of two reasons:
Microsoft management may not see any commercial value (to Microsoft) in doing this.
The C# language designers may have wanted to discourage the (what they see as) misuse of a C# preprocessor. For example, (IMO) stripping conditional compiled code out of a C# is bad for almost everyone, and using a C# preprocessor on source code that isn't C# is a probably a bad idea.
They may have wanted to avoid giving a free "leg up" to folks who want to create new 3rd-party languages based C# syntax.
(OK - I don't know if Microsoft / the C# language designers actually hold these views. Nobody does. However, these are plausible explanations for Microsoft not doing this, and plausible explanations are all that you can ever get for unexplained decisions by people you cannot "interrogate".)
I don't know enough about C# to comment on whether ConditionalAttribute and the like cause problems. However, it seems like a work-around could be found; e.g. a preprocessor command line option to re-inject #defines into the preprocessed output.

- 698,415
- 94
- 811
- 1,216