How does C# compile the code with #if preprocessor directive? - c#

I wonder how to compiler compile my code if I using #if directive inside my code.
I would like to create special version of my application (commercial demo version) and I want to limit the functionality of my application. I would rather avoid the obfuscation and just don't want to add all my compiled code to executable file. I need solution resists preview my code during disassembly process. Can I use #if directives for variant compilation instead making comments for disabling code parts?

Using the #if directive is like using the preprocessor in c++, in that the code would simply not be present if the condition hasn't been met (in compilation time). From MSDN:
When the C# compiler encounters an #if
directive, followed eventually by an
#endif directive, it will compile the code between the directives only if
the specified symbol is defined.
Unlike C and C++, you cannot assign a
numeric value to a symbol; the #if
statement in C# is Boolean and only
tests whether the symbol has been
defined or not.

As an addition to #ohadsc's answer: you can always check with Reflector what is actually produced by the compiler.

Related

Conditional resource compile

I have c# application. The application includes resource file with images and icon.
My target is to compile the same application but with different set of images/icons. Same images name, but different content.
Is there a way to include different resource file in compile time on condition?
Maybe you are looking for Preprocessor Directives or Conditional Attribute.
Preprocessor directives
From this tutorial by Bipin Joshi:
C# preprocessor directives are commands that are meant for the C# compiler. Using preprocessor directives you instruct the C# compiler to alter the compilation process in some way. For example you may instruct the C# compiler that a particular block of code be excluded from the compilation process.
ConditionalAttribute
From MSDN
Indicates to compilers that a method call or attribute should be ignored unless a specified conditional compilation symbol is defined.
To compare these two see this post.

Where to specify preprocessor directives in visual studio?

Trying to define a preprocessor directives in the Visual studio 2012.
#define FLAG
....
#endif
But not sure, where to include this FLAG in visual studio - C#. I remember specifying something like this in C++ projects.
Any thoughts ?
You have two options as to where to define it:
Code file level - In the beginning of the file write #define FLAG. You cannot place anything else (other than comments and blank lines) before define directives. As Ron Beyer points out, a directive defined in a file exists only for that file.
Project level - Right click in the project in Solution Explorer, select Properties, then the Build tab, then look at Conditional compilation symbols. Then one can define several comma-separated symbols there such as: FLAG,FOO,BAR. Note that this symbols list is project configuration dependent (there is a configuration selector in the same tab).
Note as mentioned in the comments, define does not work in C# the same way that it does in C. In C# you just declare that the symbol exists, but you can't assign a value to it. Hence the only use for these symbols is for #if FLAG directives and for the Conditional attribute.
For some reason, when properties clicked, nothing happened for me. So I did the following. And it worked.
Open your csproj project file using text editor and add your preprocessor compiler directives in between these.
<DefineConstants>
HERE!!!
</DefineConstants>

How do I set a #define for each file?

What exactly is the purpose of #define in C#?
I currently develop an application using the ShareFile .NET API from https://github.com/citrix/ShareFile-NET
However, the samples don't run because the function which I want to call is in this block:
#if Async
#endif
I know I can do
#define Async
at the top of the files, but do I have to do this for every file which has functions in this #if block? What's the purpose of this?
do I have to do this for every file?
No. You can pass symbols ("defines") to the compiler. Using Visual Studio, you can set that on the project level, at Project -> Properties -> Build -> Conditional compilation symbols.
Using the command-line C# compiler (csc) you use the /define: flag, and when using other IDEs or compilers, see their documentation.

Compilation issue in C# .NET

I have the following code (inherited from a contractor):
public partial class StoredProcedures
{
#if NO_THREAD
readonly static String version = "XXXX, Version 1.02, Apr/29/2010";
#else
readonly static String version = "XXXX, Version 0.93, Dec/21/2006";
#endif
I can't seem to find NO_THREAD anywhere else. This is code that compiles and installs as a SQL assembly. Is it something special or am I missing something simple?
Try to check Project Properties->Build->General->Conditional compilation symbols for all Build configurations which you have for the project, It may be there.
Look for a #define statement. See the docs for #if preprocessor conditionals : http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx
If you can't find a
define #NO_THREAD
Anywhere in the code, then it's probably because the contractor was defining the symbol by passing the /define compiler option.
See here for more details (typing from a cell, sorry for the format):
http://msdn.microsoft.com/en-us/library/0feaad6z.aspx
you should probably have a look at the c# pre-processor directives
No_Thread here is a symbol which can be defined by using #define No_Thread and when #define No_Thread is present then #if NO_THREAD will result in true and at compile time readonly static String version = "XXXX, Version 1.02, Apr/29/2010"; this statement will be compiled otherwise the next statement will be compiled.
this is generally used to differentiate between debug and release versions. have you noticed there are 2 versions in VS when you create a new project. if you write something like this somewhere in you code
#if DEBUG
Console.WriteLine("DEBUG VERSION");
#endif
then the string "DEBUG VERSION" would only be printed on the console when the project is in debug mode because the VS inserts a symbol DEBUG if you manually do it using the #define pre-processor then too this line would be compiled
NO_THREAD is a symbol for conditional compilation.
It can come from, #define NO_THREAD, from the project file, or from the nant file (or whatever method you use for building).
If it's defined, the first line of code is counted as part of the C# code. If it isn't, then the second is.
If that's the sole occurence, I'd say it was a hangover from something removed, but if you're uesd to using visual studio to build, then make sure there isn't a build file for nant in case the previous developer used that instead.
This is a conditional compilation symbol. In Visual Studio 2010, these appear on the Build page of your Project Properties in the Conditional compilation symbols text box. Probably one of your Configuration Manager configurations either contains this symbol or has at some point in the past. Presumably, there is another #if somewhere that disables a block of code that uses multiple threads if the NO_THREAD symbol is present.

How can I easily exclude certain lines of code from a compile?

Scattered throughout a software project I am working on are many lines of code that were written for debugging and utility purposes. Before I compile my code, I want a way to select whether or not these blocks of code should be included into my compile (something that won't require navigating the code commenting out). How can I do this?
I'm programming in c# and using Microsoft Visual Studio 2010.
Add the attribute [Conditional("DEBUG")] onto methods you only want to have execute in your debug build. See here for more detailed information.
I would suggest enclosing your blocks in #ifdef SOMETHING and #endif, and then defining SOMETHING in your project settings when you want to include that block in your compile.
You need preprocessor directives, or conditional compile statements. You can read about them here.
An example from that link:
#define TEST
using System;
public class MyClass
{
public static void Main()
{
#if (TEST)
Console.WriteLine("TEST is defined");
#else
Console.WriteLine("TEST is not defined");
#endif
}
}
The code is only compiled if TEST is defined at the top of the code. A lot of developers use #define DEBUG so they can enable debugging code and remove it again just by altering that one line at the top.
Consider using the Debug class to conditionally log, assert, etc. There are many advantages to this. You can choose to log (or not) at runtime. They limit you to (mostly) non-behavior-changing actions, addressing some of #STW's (valid) concern. They allow the use of third-party logging tools.
If they are for debugging, then the only acceptable solution is to surround such code with:
#ifdef DEBUG
#endif
This ensures that the code is included when you compile in debug mode but excluded in release mode.
You can use preprocessor directives w/ #if
You may want to consider moving these debugging functions out of the classes entirely--having your classes "change shape" between Debug and Release mode can be a real headache and can be difficult to diagnose problems.
You could consider creating a seperate "Debug" assembly which contains all your debugging helpers--then just make sure you can exclude it from the solution and build successfully without it.

Categories