尝试在 C# 中使用三元运算符时出错

发布于 2024-10-04 23:07:53 字数 378 浏览 7 评论 0原文

这是我的代码:

public void ToggleCheckBox()
{   
    if (chkSelected.Checked) ? chkSelected.Checked = false : chkSelected.Checked = true;

    //This works, but I want to write it using a ternary.
    if (chkSelected.Checked)
    {
        chkSelected.Checked = false;
    }
    else
    {
        chkSelected.Checked = true;
    }
}

我搞砸了什么?谢谢!

Here's my code:

public void ToggleCheckBox()
{   
    if (chkSelected.Checked) ? chkSelected.Checked = false : chkSelected.Checked = true;

    //This works, but I want to write it using a ternary.
    if (chkSelected.Checked)
    {
        chkSelected.Checked = false;
    }
    else
    {
        chkSelected.Checked = true;
    }
}

What did I mess up on? Thanks!

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

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

发布评论

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

评论(6

画▽骨i 2024-10-11 23:07:53

要么执行此操作:

if (chkSelected.Checked) chkSelected.Checked = false; else chkSelected.Checked = true;

...或执行此操作:

chkSelected.Checked = chkSelected.Checked ? false : true;

或放弃检查并执行此操作:

chkSelected.Checked = !chkSelected.Checked;

Either do this:

if (chkSelected.Checked) chkSelected.Checked = false; else chkSelected.Checked = true;

...or this:

chkSelected.Checked = chkSelected.Checked ? false : true;

Or abandon the check and do this:

chkSelected.Checked = !chkSelected.Checked;
心清如水 2024-10-11 23:07:53

将其写为

chkSelected.Checked = !chkSelected.Checked

要重写您的确切示例,它会变得像这样混乱:

chkSelected.Checked =  (chkSelected.Checked) ? false : true;

在本例中, ?: 运算符返回 truefalse。它无法执行分配。

Write this as

chkSelected.Checked = !chkSelected.Checked

instead.

To rewrite your exact example, it get's messy like this:

chkSelected.Checked =  (chkSelected.Checked) ? false : true;

The ?: operator returns, in this case, either trueor false. It cannot perform assignment.

删除会话 2024-10-11 23:07:53

您使用的是赋值而不是比较,并且那里不需要 if 。事实上,目前还不清楚您的意图是什么,但我猜是:

chkSelected.Checked = !chkSelected.Checked;

You're using assignment instead of comparison, and you don't need an if there. In fact, it isn't too clear what your intent is, but I'd guess it is:

chkSelected.Checked = !chkSelected.Checked;
这样的小城市 2024-10-11 23:07:53

为什么不这样做:

chkSelected.Checked = !chkSelected.Checked;

另外,它无法编译,因为您在它前面放置了 if 。删除它,它也会起作用!

Why not do:

chkSelected.Checked = !chkSelected.Checked;

Also, it does not compile, because of the if you put in front of it. Remove that and it will work as well!

苍暮颜 2024-10-11 23:07:53

我认为你的意思是:

chkSelected.Checked=(chkSelected.Checked ? false : true)

可以缩短为:

chkSelected.Checked=!chkSelected.Checked

I think you mean:

chkSelected.Checked=(chkSelected.Checked ? false : true)

which can be shortened to:

chkSelected.Checked=!chkSelected.Checked
满身野味 2024-10-11 23:07:53

因为你必须像赋值一样使用:

 chkSelected.Checked =(chkSelected.Checked ? false : true);

三元运算符就像赋值一样使用

并不是一个好主意..

chkSelected.Checked =!chkSelected.Checked;

但是在这里这样做并做更短的方式

Because you have to use like a assignation:

 chkSelected.Checked =(chkSelected.Checked ? false : true);

Ternary operator is used like a assignation

But here It is not really a good idea to do that here and do

chkSelected.Checked =!chkSelected.Checked;

Shorter way..

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