Tutorial by Examples

When the following is compiled, it will return a different value depending on which directives are defined. // Compile with /d:A or /d:B to see the difference string SomeFunction() { #if A return "A"; #elif B return "B"; #else return "C"; #endif ...
Compiler warnings can be generated using the #warning directive, and errors can likewise be generated using the #error directive. #if SOME_SYMBOL #error This is a compiler Error. #elif SOME_OTHER_SYMBOL #warning This is a compiler Warning. #endif
A compiler symbol is a keyword that is defined at compile-time that can be checked for to conditionally execute specific sections of code. There are three ways to define a compiler symbol. They can be defined via code: #define MYSYMBOL They can be defined in Visual Studio, under Project Propert...
Use #region and #endregion to define a collapsible code region. #region Event Handlers public void Button_Click(object s, EventArgs e) { // ... } public void DropDown_SelectedIndexChanged(object s, EventArgs e) { // ... } #endregion These directives are only beneficial whe...
Line #line controls the line number and filename reported by the compiler when outputting warnings and errors. void Test() { #line 42 "Answer" #line filename "SomeFile.cs" int life; // compiler warning CS0168 in "SomeFile.cs" at Line 42 #line defa...
Adding a Conditional attribute from System.Diagnostics namespace to a method is a clean way to control which methods are called in your builds and which are not. #define EXAMPLE_A using System.Diagnostics; class Program { static void Main() { ExampleA(); // This method will ...
You can disable compiler warnings using #pragma warning disable and restore them using #pragma warning restore: #pragma warning disable CS0168 // Will not generate the "unused variable" compiler warning since it was disabled var x = 5; #pragma warning restore CS0168 // Will gene...
It is convenient to set custom conditional preprocessing at project level when some actions need to be skipped lets say for tests. Go to Solution Explorer -> Click Right Mouse on project you want to set variable to -> Properties -> Build -> In General find field Conditional compilation ...

Page 1 of 1