Console.WriteLine 语句中的错误在哪里?

发布于 2024-08-03 03:47:23 字数 1308 浏览 4 评论 0原文

如果运行以下代码,您将得到输出:

答案是:


class Program
{
    static void Main(string[] args)
    {
        HtmlElement element = new HtmlElement();
        element.InnerHtml = "<br>";

        string val = element.InnerHtml != null ? element.InnerHtml : element.InnerText != null ? element.InnerText : element.TagName;
        Console.WriteLine("The answer is: "+val); // correct
        Console.WriteLine("The answer is: " +element.InnerHtml !=null ? element.InnerHtml : element.InnerText != null ? element.InnerText : element.TagName); // bug?
        Console.ReadLine();

    }
}
public class HtmlElement
{
    public string InnerHtml { get; set; }
    public string InnerText { get; set; }
    public string TagName { get; set; }
}

第二行发生了什么,预计是:

答案是:

只是为了向任何想知道的冲浪者澄清答案:

Console.WriteLine("The answer is: " +element.InnerHtml !=null ? element.InnerHtml : element.InnerText != null ? element.InnerText : element.TagName); // bug?

被评估为

Console.WriteLine(("The answer is : " + element.InnerHtml != null) ? element.InnerHtml : element.InnerText != null ? element.InnerText : element.TagName); // bug?

If you run the following code you get the output:

The answer is: <br>
<br>

class Program
{
    static void Main(string[] args)
    {
        HtmlElement element = new HtmlElement();
        element.InnerHtml = "<br>";

        string val = element.InnerHtml != null ? element.InnerHtml : element.InnerText != null ? element.InnerText : element.TagName;
        Console.WriteLine("The answer is: "+val); // correct
        Console.WriteLine("The answer is: " +element.InnerHtml !=null ? element.InnerHtml : element.InnerText != null ? element.InnerText : element.TagName); // bug?
        Console.ReadLine();

    }
}
public class HtmlElement
{
    public string InnerHtml { get; set; }
    public string InnerText { get; set; }
    public string TagName { get; set; }
}

What's happened to the second line, which is expected to be:

The answer is: <br>

Just to clarify on the answer for any wondering surfer:

Console.WriteLine("The answer is: " +element.InnerHtml !=null ? element.InnerHtml : element.InnerText != null ? element.InnerText : element.TagName); // bug?

is being evaluated as

Console.WriteLine(("The answer is : " + element.InnerHtml != null) ? element.InnerHtml : element.InnerText != null ? element.InnerText : element.TagName); // bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魔法唧唧 2024-08-10 03:47:23

您的 答案是:“ +element.InnerHtml 优先于 != null

重写为:(

Console.WriteLine("The answer is: " + (element.InnerHtml !=null ? element.InnerHtml : element.InnerText != null ? element.InnerText : element.TagName)); // bug?

添加括号)

Your The answer is: " +element.InnerHtml has precedence over the != null.

Rewrite as:

Console.WriteLine("The answer is: " + (element.InnerHtml !=null ? element.InnerHtml : element.InnerText != null ? element.InnerText : element.TagName)); // bug?

(add parenthesis)

丑丑阿 2024-08-10 03:47:23

这是一个优先级问题。你的语句:

"The answer is: " + element.InnerHtml !=null ? element.InnerHtml : ...

应该评估为:

"The answer is: " + (element.InnerHtml !=null ? element.InnerHtml : ...)
                    <------------------- this first ------------------->

但实际上评估为:

("The answer is: " + element.InnerHtml) !=null ? element.InnerHtml : ...
<------------- this first ------------>

最后一个表达式将始终为真,因为 "string" + everything != null,所以你总是会得到 element.InnerHtml (
)

在第一次测试中没有出现问题的原因是您单独评估 val ,因此手动控制优先级。

说一句,我喜欢人们似乎假设他们在一个被无数其他人使用的软件中发现了一个错误,而不是考虑他们可能做错了什么的最轻微的可能性:-)

顺便 老实说,我在很多人使用的一款产品中发现了错误(如果我没记错的话,是 Microsoft COBOL 编译器),但这是由于签名/未签名问题造成的,而且我拥有一台拥有超过 512K RAM 的最早的机器(其中检查启动代码以确保您有足够的内存,并将 640K 视为负数)。

尽管如此,四分之一个世纪中发现的错误还是相当低的,所以你不应该立即假设软件有问题。我的第一个想法总是我不知何故吃饱了(而且我通常是对的,这让我很懊恼)。

It's a precedence problem. Your statement:

"The answer is: " + element.InnerHtml !=null ? element.InnerHtml : ...

should be evaluating as:

"The answer is: " + (element.InnerHtml !=null ? element.InnerHtml : ...)
                    <------------------- this first ------------------->

but is actually evaluating as:

("The answer is: " + element.InnerHtml) !=null ? element.InnerHtml : ...
<------------- this first ------------>

That last expression will always be true, since "string" + anything != null, so you'll always get just the element.InnerHtml (<br>).

The reason you don't have the problem in the first test is because you're evaluating val separately, hence manually controlling the precedence.

As an aside, I love the way people seem to assume they've found a bug in a piece of software being used by untold millions of other people, rather than considering the slightest possibility that they may have done something wrong :-)

Although to be honest, I have found bugs in one product used by many people (a Microsoft COBOL compiler if I remember correctly) but that was due to signed/unsigned issues and I had one of the very earliest machines with more than 512K RAM (where the startup code checked to ensure you had enough memory and it treated 640K as some negative amount).

Still, one bug found in a quarter century is pretty low so it's not like you should assume the software is at fault immediately. My first thought is always that I've stuffed up somehow (and I'm usually right, much to my chagrin).

如梦初醒的夏天 2024-08-10 03:47:23

因为还没有人回答 ??

Console.WriteLine("The answer is: " + ( element.InnerHtml ?? 
     element.InnerText ?? element.TagName ) );

这需要与 ?: 方法相同的一对额外 ()

Because nobody answered with ?? yet:

Console.WriteLine("The answer is: " + ( element.InnerHtml ?? 
     element.InnerText ?? element.TagName ) );

Which needs the same pair of extra () as the ?: approach.

倒数 2024-08-10 03:47:23

两条写入线写入不同结果的原因是逻辑中的一个微妙错误。
(优先级规则与您期望的不同,如其他地方所述)

该行:

string val = element.InnerHtml != null ? element.InnerHtml : element.InnerText != null ? element.InnerText : element.TagName;

评估 element.InnerHtml 是否与 null 不同,其中该行:

Console.WriteLine("The answer is: " +element.InnerHtml !=null ? element.InnerHtml : element.InnerText != null ? element.InnerText : element.TagName); // bug?

评估“答案是:” +element.InnerHtml 是否与 null 不同,它永远不会,所以结果将始终是 element.InnerHtml (并且文本“答案是”被视为布尔表达式的一部分,因此不会被打印)

The reason why the two writelines write differrent results is a subtle bug in your logic.
(the precedence rules differs from what you expect, as noted elsewhere)

the line:

string val = element.InnerHtml != null ? element.InnerHtml : element.InnerText != null ? element.InnerText : element.TagName;

evaluates if element.InnerHtml is different from null where as the line:

Console.WriteLine("The answer is: " +element.InnerHtml !=null ? element.InnerHtml : element.InnerText != null ? element.InnerText : element.TagName); // bug?

evaluates whether "The answer is: " +element.InnerHtml is different from null, which it will never be, so the result will always be element.InnerHtml (and the text 'The answer is' is considered a part of the boolean expression and will therefore not be printed)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文