C#源代码中的#if,#else,#endif [英] #if, #else, #endif in C# source code

查看:104
本文介绍了C#源代码中的#if,#else,#endif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从MSDN中获得了一个示例代码,并且发现了以前从未见过的代码语法:

I've got a sample code from MSDN and I've found code syntax I've 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;
            }

            [...]
        }

$ if , #else #endif

什么是 #if InMemDB

什么是 InMemDB

推荐答案

这些变量称为预处理程序指令,并且自.NET 1.0开始存在。它们允许您定义不同的编译指令,例如 InMemDB ,如果已定义此变量,编译器将对块进行评估。 #if指令的文档提供了更深入的概述

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

为了定义变量,您可以使用 / define 编译器选项,或在Visual Studio项目属性的 Build 选项卡中使用 Conditional编译符号

In order to define a variable you could use the /define compiler option or use the Conditional compilation symbols in the Build tab of the properties of the project in Visual Studio:

这篇关于C#源代码中的#if,#else,#endif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆