“奇怪” C# 语法
我刚刚在从代码项目下载的一个项目中看到了这一点:
base.DialogResult = this.Result != null;
我不认为自己是 C# 新手,但这个对我来说是新的。谁能告诉我这个声明是怎么回事?
编辑很好的答案,谢谢。我以前从未使用过它。
I just saw this in a project I downloaded from Code Project:
base.DialogResult = this.Result != null;
I don't consider myself new to C# but this one is new to me. Can anyone tell me what's going on with this statement?
Edit Great answers, thanks. I've just never used that before.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果添加括号,则更容易阅读(和理解)。逻辑比较运算符
!=
位于赋值运算符=
之前:相同的语句,甚至更详细:
If you add parens it's easier to read (and understand). The logical comparison operator
!=
precedes the assignment operator=
:The same statement, even more verbose:
this.Result != null
计算结果为布尔值、true
或false
。评估的结果在基类的
DialogResult
成员中设置。一点也不奇怪,只是一个任务而已。
this.Result != null
evaluates to a boolean,true
orfalse
.The result of the evaluation is set in the
DialogResult
member of the base class.Not strange at all, it's just an assignment.
!=
(不等于)运算符优先于=
(赋值)运算符。The
!=
(not equal) operator has precedence over the=
(assignment) operator.将表达式的结果分配
给表达式,因此它返回 true 或 false,具体取决于 this.Result 是否为 null
很简单,基本上它使用不等运算符
Thats simple, basically it assigns the result of the expression
to
the expression uses the in-equality operator, so it returns either true or false, depending on wether this.Result is null or not
这意味着:
That means: