How do I set a conditional compile variable?
In C/C++ you can define macros in code like this:
#define OLD_WAY 1
Although I've never done it, I assume that the same thing is available in C#. More to the point, in C/C++ it is possible to then do some conditional compilation logic by doing something like this:
#if OLD_WAY == 1
int i = 0;
#else
int i = 1;
#endif
OK, so this is all cool and all that. And again, I assume that such logic is possible within C#. What I'd like to know is, how do I define constants at the project level, so that I can put in logic that will allow me to conditional compile one block of code if I define the constant one way, or another block of code if I don't define it that way? I'm assuming that it's done somewhere in the project's properties, but how and where do I define it?
c#
add a comment |
In C/C++ you can define macros in code like this:
#define OLD_WAY 1
Although I've never done it, I assume that the same thing is available in C#. More to the point, in C/C++ it is possible to then do some conditional compilation logic by doing something like this:
#if OLD_WAY == 1
int i = 0;
#else
int i = 1;
#endif
OK, so this is all cool and all that. And again, I assume that such logic is possible within C#. What I'd like to know is, how do I define constants at the project level, so that I can put in logic that will allow me to conditional compile one block of code if I define the constant one way, or another block of code if I don't define it that way? I'm assuming that it's done somewhere in the project's properties, but how and where do I define it?
c#
add a comment |
In C/C++ you can define macros in code like this:
#define OLD_WAY 1
Although I've never done it, I assume that the same thing is available in C#. More to the point, in C/C++ it is possible to then do some conditional compilation logic by doing something like this:
#if OLD_WAY == 1
int i = 0;
#else
int i = 1;
#endif
OK, so this is all cool and all that. And again, I assume that such logic is possible within C#. What I'd like to know is, how do I define constants at the project level, so that I can put in logic that will allow me to conditional compile one block of code if I define the constant one way, or another block of code if I don't define it that way? I'm assuming that it's done somewhere in the project's properties, but how and where do I define it?
c#
In C/C++ you can define macros in code like this:
#define OLD_WAY 1
Although I've never done it, I assume that the same thing is available in C#. More to the point, in C/C++ it is possible to then do some conditional compilation logic by doing something like this:
#if OLD_WAY == 1
int i = 0;
#else
int i = 1;
#endif
OK, so this is all cool and all that. And again, I assume that such logic is possible within C#. What I'd like to know is, how do I define constants at the project level, so that I can put in logic that will allow me to conditional compile one block of code if I define the constant one way, or another block of code if I don't define it that way? I'm assuming that it's done somewhere in the project's properties, but how and where do I define it?
c#
c#
asked Oct 1 '10 at 19:55
RodRod
1,46153358
1,46153358
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
The C# compiler csc.exe
and the C# language itself do not expose any predefined constants for conditional compilation. Visual Studio only adds the DEBUG
and TRACE
values, which can be configured through the IDE. The IDE also lets you add your own arbitrary symbols, but since these are essentially fixed (invariant) values, the capability is of limited use.
More powerful custom options can set up by manually editing your .csproj
project file. You can set up conditions here to selectively propagate conditional compilation symbols into C# based on the huge amount of environment and configuration information available in MSBuild (see here and here, but in principle, there can be no complete list, since disparate components arbitrarily contribute metadata ad-hoc).
Let's consider a working example. One case where it's useful to conditionally compile is if you want to write code that adapts to the whatever tools are discovered during the build. This way you can exploit the latest language features while still preserving the ability to compile on machines with older tooling which would, as expected, reject the alien syntax and/or keywords. For the particular case of C# 7.0 in Visual Studio 2017 we can modify the .csproj
as follows:
.csproj file (excerpt):
You could also identify each of the older C# compilers as well, degrading gracefully along the way. The same goes for detecting the .NET Framework version (oft-requested on StackOverflow [1]
[2]
[3]
[4]) and any other ambient build conditions. Such are left as exercises for the reader, but in case you want to copy/paste the highlighted lines from above, here is the text version. As an update over the screenshot, I added single-quotes to the conditional expression here (even though everything seemed to work without them)
<DefineConstants Condition="'$(VisualStudioVersion)'=='15'">CSHARP7</DefineConstants>
<!-- ... -->
<DefineConstants>DEBUG;TRACE;$(DefineConstants)</DefineConstants>
<!-- ... -->
<DefineConstants>TRACE;$(DefineConstants)</DefineConstants>
Anyway, in this manner you can now write conditional C# code using #if… #elif… #else… #endif
. Continuing the example case, the code below uses new tuple syntax--only available in C# 7--to swap array elements. Incidentally, the tuple version is not only more concise and/or elegant; it also produces excellent IL code:
#if CSHARP7
(rg[i], rg[j]) = (rg[j], rg[i]); // swap elements: tuple syntax
#else
var t = rg[i]; // swap elements: clunky
rg[i] = rg[j];
rg[j] = t;
#endif
Note that the Visual Studio
IDE does correctly process your manual .csproj
customizations in every regard. Given the .csproj
I showed earlier, the IDE code editor properly recognizes and evaluates conditional compilation for the purposes of IntelliSense
, refactoring
, "dimming-out" inactive blocks of code, etc.
I also mentioned that MSBuild
has a treasure trove of information available, of which $(VisualStudioVersion)
was just one example. Unfortunately, there's no easy to find out which values are available and what values they might have at buildtime. A trick is to temporarily put a C++
project into your Visual Studio
solution (if you don't already have one) alongside your C# project. If you right click the project properties for this .vcxproj
and then look at (e.g.) "Additional Include Directories" on the C/C++
page, a dropdown will appear at the far right when you click to edit:
You'll get a dialog box with a "Macros" button which you can click to get a list of all the available MSBuild
variables plus their expected values according to platform and configuration that are currently selected in the IDE. Don't overlook the well-known-item-metadata fields (prefixed with %
) at the bottom of the list.
You can get an idea for how much stuff is here from the size of the scrollbar thumb in this screenshot. (They're listed alphabetically, I just scrolled to this part of the 'P' section because it had minimal personal info). It's important to note, however, that both the (available) variables and their values evolve over time during the course of the build, so you may find items in this list that aren't available to your .csproj
at the time it's processed.
[edit:] Another way to find out what property values are available during and throughout your build process is to set the MSBuild "output verbosity" to "Detailed", and then rebuild.
After the build finishes, examine the top of the build log in the Visual Studio Output Window, and you'll see a list of the available property names along with their initial values.
Awsome! This answer needs more votes! Especially for the C++ hint :-)
– Matt
May 12 '17 at 13:16
add a comment |
In C# you can do #define
but you can't use values on them like you can in C++.
Each define can have 2 states: defined or undefined
In the project properties under Build you can set defines that should be defined.
Anything you specify here will be defined across all of your project files.
So for example I can define 2 conditional compilation symbols in this field as:MY_DEFINE1, MY_DEFINE2
Then in my code I can do things like this:
#if MY_DEFINE1
//do something conditionally
#endif
#if MY_DEFINE2
//do something else conditionally
#endif
Alternatively you can do your defines per file, but unlike C++ they must be at the top of your file.
At the top of your file you can use:
#define MY_DEFINE2
or you can at the top of your file use:
#undef MY_DEFINE2
This last one you'd do if you set a conditional compilation symbol and you wanted it in all files except maybe one.
add a comment |
Open your project properties and look at the Build page. There is a box called Conditional compilation symbols.
3
@toddmo What are you talking about? It's in exactly the same spot in VS2015 as it has been in every version for 15 years.
– Matthew Ferreira
Nov 5 '16 at 16:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3842590%2fhow-do-i-set-a-conditional-compile-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The C# compiler csc.exe
and the C# language itself do not expose any predefined constants for conditional compilation. Visual Studio only adds the DEBUG
and TRACE
values, which can be configured through the IDE. The IDE also lets you add your own arbitrary symbols, but since these are essentially fixed (invariant) values, the capability is of limited use.
More powerful custom options can set up by manually editing your .csproj
project file. You can set up conditions here to selectively propagate conditional compilation symbols into C# based on the huge amount of environment and configuration information available in MSBuild (see here and here, but in principle, there can be no complete list, since disparate components arbitrarily contribute metadata ad-hoc).
Let's consider a working example. One case where it's useful to conditionally compile is if you want to write code that adapts to the whatever tools are discovered during the build. This way you can exploit the latest language features while still preserving the ability to compile on machines with older tooling which would, as expected, reject the alien syntax and/or keywords. For the particular case of C# 7.0 in Visual Studio 2017 we can modify the .csproj
as follows:
.csproj file (excerpt):
You could also identify each of the older C# compilers as well, degrading gracefully along the way. The same goes for detecting the .NET Framework version (oft-requested on StackOverflow [1]
[2]
[3]
[4]) and any other ambient build conditions. Such are left as exercises for the reader, but in case you want to copy/paste the highlighted lines from above, here is the text version. As an update over the screenshot, I added single-quotes to the conditional expression here (even though everything seemed to work without them)
<DefineConstants Condition="'$(VisualStudioVersion)'=='15'">CSHARP7</DefineConstants>
<!-- ... -->
<DefineConstants>DEBUG;TRACE;$(DefineConstants)</DefineConstants>
<!-- ... -->
<DefineConstants>TRACE;$(DefineConstants)</DefineConstants>
Anyway, in this manner you can now write conditional C# code using #if… #elif… #else… #endif
. Continuing the example case, the code below uses new tuple syntax--only available in C# 7--to swap array elements. Incidentally, the tuple version is not only more concise and/or elegant; it also produces excellent IL code:
#if CSHARP7
(rg[i], rg[j]) = (rg[j], rg[i]); // swap elements: tuple syntax
#else
var t = rg[i]; // swap elements: clunky
rg[i] = rg[j];
rg[j] = t;
#endif
Note that the Visual Studio
IDE does correctly process your manual .csproj
customizations in every regard. Given the .csproj
I showed earlier, the IDE code editor properly recognizes and evaluates conditional compilation for the purposes of IntelliSense
, refactoring
, "dimming-out" inactive blocks of code, etc.
I also mentioned that MSBuild
has a treasure trove of information available, of which $(VisualStudioVersion)
was just one example. Unfortunately, there's no easy to find out which values are available and what values they might have at buildtime. A trick is to temporarily put a C++
project into your Visual Studio
solution (if you don't already have one) alongside your C# project. If you right click the project properties for this .vcxproj
and then look at (e.g.) "Additional Include Directories" on the C/C++
page, a dropdown will appear at the far right when you click to edit:
You'll get a dialog box with a "Macros" button which you can click to get a list of all the available MSBuild
variables plus their expected values according to platform and configuration that are currently selected in the IDE. Don't overlook the well-known-item-metadata fields (prefixed with %
) at the bottom of the list.
You can get an idea for how much stuff is here from the size of the scrollbar thumb in this screenshot. (They're listed alphabetically, I just scrolled to this part of the 'P' section because it had minimal personal info). It's important to note, however, that both the (available) variables and their values evolve over time during the course of the build, so you may find items in this list that aren't available to your .csproj
at the time it's processed.
[edit:] Another way to find out what property values are available during and throughout your build process is to set the MSBuild "output verbosity" to "Detailed", and then rebuild.
After the build finishes, examine the top of the build log in the Visual Studio Output Window, and you'll see a list of the available property names along with their initial values.
Awsome! This answer needs more votes! Especially for the C++ hint :-)
– Matt
May 12 '17 at 13:16
add a comment |
The C# compiler csc.exe
and the C# language itself do not expose any predefined constants for conditional compilation. Visual Studio only adds the DEBUG
and TRACE
values, which can be configured through the IDE. The IDE also lets you add your own arbitrary symbols, but since these are essentially fixed (invariant) values, the capability is of limited use.
More powerful custom options can set up by manually editing your .csproj
project file. You can set up conditions here to selectively propagate conditional compilation symbols into C# based on the huge amount of environment and configuration information available in MSBuild (see here and here, but in principle, there can be no complete list, since disparate components arbitrarily contribute metadata ad-hoc).
Let's consider a working example. One case where it's useful to conditionally compile is if you want to write code that adapts to the whatever tools are discovered during the build. This way you can exploit the latest language features while still preserving the ability to compile on machines with older tooling which would, as expected, reject the alien syntax and/or keywords. For the particular case of C# 7.0 in Visual Studio 2017 we can modify the .csproj
as follows:
.csproj file (excerpt):
You could also identify each of the older C# compilers as well, degrading gracefully along the way. The same goes for detecting the .NET Framework version (oft-requested on StackOverflow [1]
[2]
[3]
[4]) and any other ambient build conditions. Such are left as exercises for the reader, but in case you want to copy/paste the highlighted lines from above, here is the text version. As an update over the screenshot, I added single-quotes to the conditional expression here (even though everything seemed to work without them)
<DefineConstants Condition="'$(VisualStudioVersion)'=='15'">CSHARP7</DefineConstants>
<!-- ... -->
<DefineConstants>DEBUG;TRACE;$(DefineConstants)</DefineConstants>
<!-- ... -->
<DefineConstants>TRACE;$(DefineConstants)</DefineConstants>
Anyway, in this manner you can now write conditional C# code using #if… #elif… #else… #endif
. Continuing the example case, the code below uses new tuple syntax--only available in C# 7--to swap array elements. Incidentally, the tuple version is not only more concise and/or elegant; it also produces excellent IL code:
#if CSHARP7
(rg[i], rg[j]) = (rg[j], rg[i]); // swap elements: tuple syntax
#else
var t = rg[i]; // swap elements: clunky
rg[i] = rg[j];
rg[j] = t;
#endif
Note that the Visual Studio
IDE does correctly process your manual .csproj
customizations in every regard. Given the .csproj
I showed earlier, the IDE code editor properly recognizes and evaluates conditional compilation for the purposes of IntelliSense
, refactoring
, "dimming-out" inactive blocks of code, etc.
I also mentioned that MSBuild
has a treasure trove of information available, of which $(VisualStudioVersion)
was just one example. Unfortunately, there's no easy to find out which values are available and what values they might have at buildtime. A trick is to temporarily put a C++
project into your Visual Studio
solution (if you don't already have one) alongside your C# project. If you right click the project properties for this .vcxproj
and then look at (e.g.) "Additional Include Directories" on the C/C++
page, a dropdown will appear at the far right when you click to edit:
You'll get a dialog box with a "Macros" button which you can click to get a list of all the available MSBuild
variables plus their expected values according to platform and configuration that are currently selected in the IDE. Don't overlook the well-known-item-metadata fields (prefixed with %
) at the bottom of the list.
You can get an idea for how much stuff is here from the size of the scrollbar thumb in this screenshot. (They're listed alphabetically, I just scrolled to this part of the 'P' section because it had minimal personal info). It's important to note, however, that both the (available) variables and their values evolve over time during the course of the build, so you may find items in this list that aren't available to your .csproj
at the time it's processed.
[edit:] Another way to find out what property values are available during and throughout your build process is to set the MSBuild "output verbosity" to "Detailed", and then rebuild.
After the build finishes, examine the top of the build log in the Visual Studio Output Window, and you'll see a list of the available property names along with their initial values.
Awsome! This answer needs more votes! Especially for the C++ hint :-)
– Matt
May 12 '17 at 13:16
add a comment |
The C# compiler csc.exe
and the C# language itself do not expose any predefined constants for conditional compilation. Visual Studio only adds the DEBUG
and TRACE
values, which can be configured through the IDE. The IDE also lets you add your own arbitrary symbols, but since these are essentially fixed (invariant) values, the capability is of limited use.
More powerful custom options can set up by manually editing your .csproj
project file. You can set up conditions here to selectively propagate conditional compilation symbols into C# based on the huge amount of environment and configuration information available in MSBuild (see here and here, but in principle, there can be no complete list, since disparate components arbitrarily contribute metadata ad-hoc).
Let's consider a working example. One case where it's useful to conditionally compile is if you want to write code that adapts to the whatever tools are discovered during the build. This way you can exploit the latest language features while still preserving the ability to compile on machines with older tooling which would, as expected, reject the alien syntax and/or keywords. For the particular case of C# 7.0 in Visual Studio 2017 we can modify the .csproj
as follows:
.csproj file (excerpt):
You could also identify each of the older C# compilers as well, degrading gracefully along the way. The same goes for detecting the .NET Framework version (oft-requested on StackOverflow [1]
[2]
[3]
[4]) and any other ambient build conditions. Such are left as exercises for the reader, but in case you want to copy/paste the highlighted lines from above, here is the text version. As an update over the screenshot, I added single-quotes to the conditional expression here (even though everything seemed to work without them)
<DefineConstants Condition="'$(VisualStudioVersion)'=='15'">CSHARP7</DefineConstants>
<!-- ... -->
<DefineConstants>DEBUG;TRACE;$(DefineConstants)</DefineConstants>
<!-- ... -->
<DefineConstants>TRACE;$(DefineConstants)</DefineConstants>
Anyway, in this manner you can now write conditional C# code using #if… #elif… #else… #endif
. Continuing the example case, the code below uses new tuple syntax--only available in C# 7--to swap array elements. Incidentally, the tuple version is not only more concise and/or elegant; it also produces excellent IL code:
#if CSHARP7
(rg[i], rg[j]) = (rg[j], rg[i]); // swap elements: tuple syntax
#else
var t = rg[i]; // swap elements: clunky
rg[i] = rg[j];
rg[j] = t;
#endif
Note that the Visual Studio
IDE does correctly process your manual .csproj
customizations in every regard. Given the .csproj
I showed earlier, the IDE code editor properly recognizes and evaluates conditional compilation for the purposes of IntelliSense
, refactoring
, "dimming-out" inactive blocks of code, etc.
I also mentioned that MSBuild
has a treasure trove of information available, of which $(VisualStudioVersion)
was just one example. Unfortunately, there's no easy to find out which values are available and what values they might have at buildtime. A trick is to temporarily put a C++
project into your Visual Studio
solution (if you don't already have one) alongside your C# project. If you right click the project properties for this .vcxproj
and then look at (e.g.) "Additional Include Directories" on the C/C++
page, a dropdown will appear at the far right when you click to edit:
You'll get a dialog box with a "Macros" button which you can click to get a list of all the available MSBuild
variables plus their expected values according to platform and configuration that are currently selected in the IDE. Don't overlook the well-known-item-metadata fields (prefixed with %
) at the bottom of the list.
You can get an idea for how much stuff is here from the size of the scrollbar thumb in this screenshot. (They're listed alphabetically, I just scrolled to this part of the 'P' section because it had minimal personal info). It's important to note, however, that both the (available) variables and their values evolve over time during the course of the build, so you may find items in this list that aren't available to your .csproj
at the time it's processed.
[edit:] Another way to find out what property values are available during and throughout your build process is to set the MSBuild "output verbosity" to "Detailed", and then rebuild.
After the build finishes, examine the top of the build log in the Visual Studio Output Window, and you'll see a list of the available property names along with their initial values.
The C# compiler csc.exe
and the C# language itself do not expose any predefined constants for conditional compilation. Visual Studio only adds the DEBUG
and TRACE
values, which can be configured through the IDE. The IDE also lets you add your own arbitrary symbols, but since these are essentially fixed (invariant) values, the capability is of limited use.
More powerful custom options can set up by manually editing your .csproj
project file. You can set up conditions here to selectively propagate conditional compilation symbols into C# based on the huge amount of environment and configuration information available in MSBuild (see here and here, but in principle, there can be no complete list, since disparate components arbitrarily contribute metadata ad-hoc).
Let's consider a working example. One case where it's useful to conditionally compile is if you want to write code that adapts to the whatever tools are discovered during the build. This way you can exploit the latest language features while still preserving the ability to compile on machines with older tooling which would, as expected, reject the alien syntax and/or keywords. For the particular case of C# 7.0 in Visual Studio 2017 we can modify the .csproj
as follows:
.csproj file (excerpt):
You could also identify each of the older C# compilers as well, degrading gracefully along the way. The same goes for detecting the .NET Framework version (oft-requested on StackOverflow [1]
[2]
[3]
[4]) and any other ambient build conditions. Such are left as exercises for the reader, but in case you want to copy/paste the highlighted lines from above, here is the text version. As an update over the screenshot, I added single-quotes to the conditional expression here (even though everything seemed to work without them)
<DefineConstants Condition="'$(VisualStudioVersion)'=='15'">CSHARP7</DefineConstants>
<!-- ... -->
<DefineConstants>DEBUG;TRACE;$(DefineConstants)</DefineConstants>
<!-- ... -->
<DefineConstants>TRACE;$(DefineConstants)</DefineConstants>
Anyway, in this manner you can now write conditional C# code using #if… #elif… #else… #endif
. Continuing the example case, the code below uses new tuple syntax--only available in C# 7--to swap array elements. Incidentally, the tuple version is not only more concise and/or elegant; it also produces excellent IL code:
#if CSHARP7
(rg[i], rg[j]) = (rg[j], rg[i]); // swap elements: tuple syntax
#else
var t = rg[i]; // swap elements: clunky
rg[i] = rg[j];
rg[j] = t;
#endif
Note that the Visual Studio
IDE does correctly process your manual .csproj
customizations in every regard. Given the .csproj
I showed earlier, the IDE code editor properly recognizes and evaluates conditional compilation for the purposes of IntelliSense
, refactoring
, "dimming-out" inactive blocks of code, etc.
I also mentioned that MSBuild
has a treasure trove of information available, of which $(VisualStudioVersion)
was just one example. Unfortunately, there's no easy to find out which values are available and what values they might have at buildtime. A trick is to temporarily put a C++
project into your Visual Studio
solution (if you don't already have one) alongside your C# project. If you right click the project properties for this .vcxproj
and then look at (e.g.) "Additional Include Directories" on the C/C++
page, a dropdown will appear at the far right when you click to edit:
You'll get a dialog box with a "Macros" button which you can click to get a list of all the available MSBuild
variables plus their expected values according to platform and configuration that are currently selected in the IDE. Don't overlook the well-known-item-metadata fields (prefixed with %
) at the bottom of the list.
You can get an idea for how much stuff is here from the size of the scrollbar thumb in this screenshot. (They're listed alphabetically, I just scrolled to this part of the 'P' section because it had minimal personal info). It's important to note, however, that both the (available) variables and their values evolve over time during the course of the build, so you may find items in this list that aren't available to your .csproj
at the time it's processed.
[edit:] Another way to find out what property values are available during and throughout your build process is to set the MSBuild "output verbosity" to "Detailed", and then rebuild.
After the build finishes, examine the top of the build log in the Visual Studio Output Window, and you'll see a list of the available property names along with their initial values.
edited Nov 22 '18 at 22:37
answered Apr 16 '17 at 20:58
Glenn SlaydenGlenn Slayden
9,11616276
9,11616276
Awsome! This answer needs more votes! Especially for the C++ hint :-)
– Matt
May 12 '17 at 13:16
add a comment |
Awsome! This answer needs more votes! Especially for the C++ hint :-)
– Matt
May 12 '17 at 13:16
Awsome! This answer needs more votes! Especially for the C++ hint :-)
– Matt
May 12 '17 at 13:16
Awsome! This answer needs more votes! Especially for the C++ hint :-)
– Matt
May 12 '17 at 13:16
add a comment |
In C# you can do #define
but you can't use values on them like you can in C++.
Each define can have 2 states: defined or undefined
In the project properties under Build you can set defines that should be defined.
Anything you specify here will be defined across all of your project files.
So for example I can define 2 conditional compilation symbols in this field as:MY_DEFINE1, MY_DEFINE2
Then in my code I can do things like this:
#if MY_DEFINE1
//do something conditionally
#endif
#if MY_DEFINE2
//do something else conditionally
#endif
Alternatively you can do your defines per file, but unlike C++ they must be at the top of your file.
At the top of your file you can use:
#define MY_DEFINE2
or you can at the top of your file use:
#undef MY_DEFINE2
This last one you'd do if you set a conditional compilation symbol and you wanted it in all files except maybe one.
add a comment |
In C# you can do #define
but you can't use values on them like you can in C++.
Each define can have 2 states: defined or undefined
In the project properties under Build you can set defines that should be defined.
Anything you specify here will be defined across all of your project files.
So for example I can define 2 conditional compilation symbols in this field as:MY_DEFINE1, MY_DEFINE2
Then in my code I can do things like this:
#if MY_DEFINE1
//do something conditionally
#endif
#if MY_DEFINE2
//do something else conditionally
#endif
Alternatively you can do your defines per file, but unlike C++ they must be at the top of your file.
At the top of your file you can use:
#define MY_DEFINE2
or you can at the top of your file use:
#undef MY_DEFINE2
This last one you'd do if you set a conditional compilation symbol and you wanted it in all files except maybe one.
add a comment |
In C# you can do #define
but you can't use values on them like you can in C++.
Each define can have 2 states: defined or undefined
In the project properties under Build you can set defines that should be defined.
Anything you specify here will be defined across all of your project files.
So for example I can define 2 conditional compilation symbols in this field as:MY_DEFINE1, MY_DEFINE2
Then in my code I can do things like this:
#if MY_DEFINE1
//do something conditionally
#endif
#if MY_DEFINE2
//do something else conditionally
#endif
Alternatively you can do your defines per file, but unlike C++ they must be at the top of your file.
At the top of your file you can use:
#define MY_DEFINE2
or you can at the top of your file use:
#undef MY_DEFINE2
This last one you'd do if you set a conditional compilation symbol and you wanted it in all files except maybe one.
In C# you can do #define
but you can't use values on them like you can in C++.
Each define can have 2 states: defined or undefined
In the project properties under Build you can set defines that should be defined.
Anything you specify here will be defined across all of your project files.
So for example I can define 2 conditional compilation symbols in this field as:MY_DEFINE1, MY_DEFINE2
Then in my code I can do things like this:
#if MY_DEFINE1
//do something conditionally
#endif
#if MY_DEFINE2
//do something else conditionally
#endif
Alternatively you can do your defines per file, but unlike C++ they must be at the top of your file.
At the top of your file you can use:
#define MY_DEFINE2
or you can at the top of your file use:
#undef MY_DEFINE2
This last one you'd do if you set a conditional compilation symbol and you wanted it in all files except maybe one.
edited Oct 1 '10 at 20:08
answered Oct 1 '10 at 19:57
Brian R. BondyBrian R. Bondy
255k98544594
255k98544594
add a comment |
add a comment |
Open your project properties and look at the Build page. There is a box called Conditional compilation symbols.
3
@toddmo What are you talking about? It's in exactly the same spot in VS2015 as it has been in every version for 15 years.
– Matthew Ferreira
Nov 5 '16 at 16:25
add a comment |
Open your project properties and look at the Build page. There is a box called Conditional compilation symbols.
3
@toddmo What are you talking about? It's in exactly the same spot in VS2015 as it has been in every version for 15 years.
– Matthew Ferreira
Nov 5 '16 at 16:25
add a comment |
Open your project properties and look at the Build page. There is a box called Conditional compilation symbols.
Open your project properties and look at the Build page. There is a box called Conditional compilation symbols.
edited Nov 8 '16 at 21:48
toddmo
9,52276375
9,52276375
answered Oct 1 '10 at 19:57
Matthew FerreiraMatthew Ferreira
6,78513451
6,78513451
3
@toddmo What are you talking about? It's in exactly the same spot in VS2015 as it has been in every version for 15 years.
– Matthew Ferreira
Nov 5 '16 at 16:25
add a comment |
3
@toddmo What are you talking about? It's in exactly the same spot in VS2015 as it has been in every version for 15 years.
– Matthew Ferreira
Nov 5 '16 at 16:25
3
3
@toddmo What are you talking about? It's in exactly the same spot in VS2015 as it has been in every version for 15 years.
– Matthew Ferreira
Nov 5 '16 at 16:25
@toddmo What are you talking about? It's in exactly the same spot in VS2015 as it has been in every version for 15 years.
– Matthew Ferreira
Nov 5 '16 at 16:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3842590%2fhow-do-i-set-a-conditional-compile-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown