<p>I'm wondering about instances when it makes sent to use #define and #if statements. I've known about it for a while, but never incorporated it into my way of coding. How exactly does this affect the compilation?</p>

<blockquote>
  <p>Is #define the only thing that determines if the code is included when compiled? If I have #define DEBUGme as a custom symbol, the only way to exclude it from compile is to remove this #define statement?</p>
</blockquote>

[{'answer_id': 15749, 'author': 'Ed S.', 'author_id': 1053, 'author_profile': 'https://Stackoverflow.com/users/1053', 'pm_score': 1, 'selected': False, 'text': "<p>Well, defines are used often for compile time constants and macros.  This can make your code a bit faster as there are really no function calls, the output values of the macros are determined at compile time.  The #if's are very useful.  The most simple example that I can think of is checking for a debug build to add in some extra logging or messaging, maybe even some debugging functions.  You can also check different environment variables this way.</p>\n\n<p>Others with more C/C++ experience can add more I am sure.</p>\n"}, {'answer_id': 15757, 'author': 'HS.', 'author_id': 1398, 'author_profile': 'https://Stackoverflow.com/users/1398', 'pm_score': 1, 'selected': False, 'text': '<p>I often find myself defining some things that are done repetitively in certain functions. That makes the code much shorter and thus allows a better overview.</p>\n\n<p>But as always, try to find a good measure to not create a new language out of it. Might be a little hard to read for the occasional maintenance later on.</p>\n'}, {'answer_id': 15758, 'author': 'Eric Haskins', 'author_id': 100, 'author_profile': 'https://Stackoverflow.com/users/100', 'pm_score': 5, 'selected': True, 'text': '<p>In C# <code>#define</code> macros, like some of Bernard\'s examples, are not allowed. The only common use of <code>#define</code>/<code>#if</code>s in C# is for adding optional debug only code. For example:</p>\n\n<pre><code>        static void Main(string[] args)\n        {\n#if DEBUG\n            //this only compiles if in DEBUG\n            Console.WriteLine("DEBUG")\n#endif \n#if !DEBUG\n            //this only compiles if not in DEBUG\n            Console.WriteLine("RELEASE")\n#endif\n            //This always compiles\n            Console.ReadLine()\n        }\n</code></pre>\n'}, {'answer_id': 15761, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 3, 'selected': False, 'text': '<p>#define is used to define compile-time constants that you can use with #if to include or exclude bits of code.</p>\n\n<pre><code>#define USEFOREACH\n\n#if USEFOREACH\n    foreach(var item in items)\n     {  \n#else\n    for(int i=0; i &lt; items.Length; ++i)\n     { var item = items[i];   //take item\n#endif\n\n       doSomethingWithItem(item);\n     }\n</code></pre>\n'}, {'answer_id': 15767, 'author': 'OJ.', 'author_id': 611, 'author_profile': 'https://Stackoverflow.com/users/611', 'pm_score': 0, 'selected': False, 'text': '<p>@Ed: When using C++, there is rarely any benefit for using #define over inline functions when creating macros. The idea of "greater speed" is a misconception. With inline functions you get the same speed, but you also get type safey, and no side-effects of preprocessor "pasting" due to the fact that parameters are evaluated before the function is called (for an example, try writing the ubiquitous MAX macro, and call it like this: MAX(x++, y).. you\'ll see what I\'m getting at).</p>\n\n<p>I have never had to use #define in my C#, and I very rarely use it for anything other that platform and compiler version checking for conditional compilation in C++.</p>\n'}, {'answer_id': 15778, 'author': 'roo', 'author_id': 716, 'author_profile': 'https://Stackoverflow.com/users/716', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>Is #define the only thing that\n  determines if the code is included\n  when compiled? If I have #define\n  DEBUGme as a custom symbol, the only\n  way to exclude it from compile is to\n  remove this #define statement?</p>\n</blockquote>\n\n<p>You can <a href="http://msdn.microsoft.com/en-us/library/wkxst87d(VS.80).aspx" rel="nofollow noreferrer">undefine</a> symbols as well</p>\n\n<pre><code>#if defined(DEBUG)\n#undef DEBUG\n#endif\n</code></pre>\n'}, {'answer_id': 15994, 'author': 'ICR', 'author_id': 214, 'author_profile': 'https://Stackoverflow.com/users/214', 'pm_score': 0, 'selected': False, 'text': '<p>Perhaps the most common usees of #define in C# is to differentiate between debug/release and different platforms (for example Windows and X-Box 360 in the XNA framework).</p>\n'}, {'answer_id': 16016, 'author': 'Ali Parr', 'author_id': 1169, 'author_profile': 'https://Stackoverflow.com/users/1169', 'pm_score': 1, 'selected': False, 'text': "<p>It's for conditional compilation, so you can include or remove bits of code based upon project attributes which tend to be:</p>\n\n<ul>\n<li>Intended platform (Windows/Linux/XB360/PS3/Iphone.... etc)</li>\n<li>Release or Debug (Generally logging, asserts etc are only included in a debug build)</li>\n</ul>\n\n<p>They can also be used to disable large parts of a system quickly,\nfor example, during development of a game, I might define </p>\n\n<pre><code>#define PLAYSOUNDS\n</code></pre>\n\n<p>and then wrap the final call to play a sound in:</p>\n\n<pre><code>#ifdef PLAYSOUNDS\n// Do lots of funk to play a sound\nreturn true;\n#else\nreturn true;\n</code></pre>\n\n<p>So it's very easy for me to turn on and off the playing of sounds for a build. (Typically I don't play sounds when debugging because it gets in the way of my personal music :) )\nThe benefit is that you're not introducing a branch through adding an if statement....</p>\n"}]
['https://Stackoverflow.com/questions/15744', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1888/']