views:

214

answers:

4

Is it possible in C# to set such a condition that if the condition is true - compile one file;If condition is false - compile another file?

Sort of like

#ifdef DEBUG
#include Class1.cs
#else
#include Class2.cs
#endif

Or possibly set it up in project properties.

+4  A: 

No, it isn't.

However, you can wrap both entire files in #if blocks.

You might also want to look at the [Conditional] attribute.

SLaks
Also don't forget that partial classes and partial methods can be extremely useful in cases like that.
DrJokepu
A: 

Thankfully no, not in the preprocessor like that. You can, however, exclude the implementation of a whole file within the file itself. Or you can set up a build process with MSBuild or nAnt to switch around our files.

Dave Markle
+1  A: 

I wouldn't recommend it. I don't like the idea of Debug and Release having such wildly different code that you need to have two totally separate files to make sense of the differences. #if DEBUG at all is a pretty big code smell IMO...

However, you could do it like this:

// Class1.cs
#if DEBUG

...

#endif

.

// Class2.cs
#if !DEBUG

...

#endif
Dean Harding
+1  A: 

In C# we don't use an include of a file, but you can use Conditional Methods.

For instance, if I'm developing a game I'm using a shared code base for my input class, but I want to have one method called if I'm on an Xbox, and a different method get called if I'm on a Zune. It's still going to return the same class of input data, but it's going to take very different route to get it.

You learn more about conditional methods here: http://msdn.microsoft.com/en-us/library/aa288458(v=VS.71).aspx

seraphym