gpt4 book ai didi

c# - undefined variable 不应该在使用#if 检查时抛出异常吗?

转载 作者:太空宇宙 更新时间:2023-11-03 17:38:04 24 4
gpt4 key购买 nike

using System;
public class Program
{
public static void Main(string[] args)
{
#if (!pi)
Console.WriteLine("i");
#else
Console.WriteLine("PI undefined");
#endif
Console.WriteLine("ok");
Console.ReadLine();
}
}

输出:

i
ok

有人能给我解释一下吗?如果没有定义pi,它不应该抛出异常吗?

最佳答案

#If 指令专门检查给定的符号是否已定义。

来自 documentation :

When the C# compiler encounters an #if directive, followed eventually by an #endif directive, it compiles the code between the directives only if the specified symbol is defined. Unlike C and C++, you cannot assign a numeric value to a symbol. The #if statement in C# is Boolean and only tests whether the symbol has been defined or not.

此外:

#define lets you define a symbol. By then using the symbol as the expression passed to the #if directive, the expression evaluates to true.

因此,如果 undefined symbol (在您的情况下为 pi),则结果始终为 false。

关于c# - undefined variable 不应该在使用#if 检查时抛出异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51037895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com