简体   繁体   English

您可以为 C# 版本添加预处理器指令吗?

[英]Can you add a preprocessor directive for a C# version?

Is there a way to add a preprocessor directive that compares the C# version used by the compiler?有没有办法添加一个预处理器指令来比较编译器使用的 C# 版本?

For example :例如 :

public T MyMethod<T>()
{
#if CS_7
    return default; // Feature 'default literal' is not available in C# 7.0. Please use 7.1 or greater.
#else
    return null; // Ignore the fact that this won't work, it isn't the point of the question
#endif
}

Would the .NET version be the way to go here? .NET 版本是通往这里的路吗? Wouldn't there be situations where the compiler used is different for the same project?不会有同一个项目使用的编译器不同的情况吗? (ie if different employess use different versions of Visual Studio) (即如果不同的员工使用不同版本的 Visual Studio)

Apparently you can't, but the C# version used depends on the framework version ( list of version per framework ) used in the project, which provides a default C# version, unless configured otherwise manually through something like the property (see linked documentation).显然你不能,但使用的 C# 版本取决于项目中使用的框架版本( 每个框架的版本列表),它提供默认的 C# 版本,除非通过类似属性的其他方式手动配置(请参阅链接文档)。

Thus, most situations where you would want a C# version preprocessor directive can either be resolved by adding a framework preprocessor directive or forcing a specific C# version in the project itself using the property in the project file.因此,您需要 C# 版本预处理器指令的大多数情况可以通过添加框架预处理器指令或使用项目文件中的属性强制项目本身中的特定 C# 版本来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM