#if, #else, #endif in C # source code

I have sample code from MSDN and I found code syntax that I had never seen before:

namespace Mvc3RemoteVal.Controllers { public class HomeController : Controller { IUserDB _repository; #if InMemDB public HomeController() : this(InMemoryDB.Instance) { } #else public HomeController() : this(new EF_UserRepository()) { } #endif public HomeController(IUserDB repository) { _repository = repository; } [...] } 

What are these #if , #else , #endif ?

What is #if InMemDB ?

What is InMemDB ? Variable?

+4
source share
2 answers

These are called preprocessor directives and exist since .NET 1.0. They allow you to define various compilation directives, such as InMemDB , and the compiler will evaluate whether or not to block if this variable is defined. The # if directive documentation provides a more in-depth overview.

To define a variable, you can use the / define option or use the conditional compilation symbols on the Assembly tab of the project properties in Visual Studio:

alt text

+14
source

These are not new features for Framework 4

these are functions that you can use for the development and testing phase: you can declare:

 #Define something 

and then

 #if something 

all the code in that the "if" will be executed. all code that is not will not.

+1
source

Source: https://habr.com/ru/post/1336676/


All Articles