1

I'm trying to learn preprocessor directive, so I made a simple example like this:

using System; namespace learning { class Program { static void Main(string[] args) { #define DEBUG test(); } private void test() { #if DEBUG Console.WriteLine("Debug mode"); #elif RELEASE Console.WriteLine("Release mode"); #else Console.WriteLine("Debug and release don't defined"); #endif } } } 

but I got unfortunately this error:

Impossible to define or cancel defining the preprocessor symbols after the first token in the file

I take this example from a programming book, what is wrong?

update (my solution):

#define RELEASE using System; namespace learning { class Program { static void Main(string[] args) { #if DEBUG Console.WriteLine("Debug mode"); #elif RELEASE Console.WriteLine("Release mode"); #else Console.WriteLine("Debug and release don't defined"); #endif Console.ReadLine(); } } } 

printed result: Debug mode, but should be Release mode. What I did wrong?

4
  • 1
    Put #define DEBUG at the beginning of the file. Commented Apr 21, 2016 at 13:39
  • The book is wrong, basically. Which book is it? Name and shame... (I'm really hoping it isn't mine...) Commented Apr 21, 2016 at 13:43
  • @JonSkeet antoniopelleriti.it/page/libro-csharp Commented Apr 21, 2016 at 13:45
  • I note that the code is invalid due to calling an instance method from a static method without an explicit target, too... Commented Apr 21, 2016 at 13:48

2 Answers 2

4

As Neo mentioned, you can define it at the top of your file.

Or, if you want for it to be global, and you are using Visual Studio, you could define it in your project properties (right-click on the project, choose Properties, then go to the Build tab).

That screen would allow you to define the symbol based upon the build configuration (so you could set your symbol for debug builds and have it automatically be removed when you switch configurations).

If you are not using Visual Studio, you could pass your conditional symbols to MSBuild or csc (the command line build or command line c# compiler). All of these options would give you the ability to set/unset symbols without changing the source code.

Sign up to request clarification or add additional context in comments.

4 Comments

Got it, but is possible define more symbols?
@Dillinger: You can define as many symbols as you like at the start of each file, and also in the project settings. You can't define more symbols once you're into "normal" code.
Yep, you can define as many as you want -- just put them in the "Conditional Compilation symbols" in the Build Tab.
Up voting as this is a more complete answer than mine.
2

You need to put the #define line at the very start of the file, before the using statements

4 Comments

Oh, there is no refence of this in the book. I got it now. But I have update the example putting #define RELEASE at the top of all, why the compiler print me: Debug mode instead of Release mode?
@Dillinger: My guess is that you're building it in Visual Studio in a debug configuration, where DEBUG is already defined in the project settings.
Have a look at my answer below, but in addition to debug symbols, you can have build configurations. This allows you set a group of options and have them apply to a specific configuration. For example, By default, you get "Debug" and "Release" configurations when you create a project. Debug has the DEBUG symbol enabled, and compiler optimizations turned off. Release turns on compile optimizations (there are other differences as well). Have a look at the Configuration Manager (right-click on the solution).
@JonSkeet You're right.. I turn on release mode and now the console print the correct result.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.