?没有其他部分的运算符 [英] ? operator without else-part

查看:119
本文介绍了?没有其他部分的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#吗?当我的if语句会影响一行并且一切都很好时。但是可以说我有以下代码(使用经典的if语句):

I use C# ? operator when I have if-statements that affects one row and it's all good. But lets say I have this code (using classic if-statements):

if(someStatement)
{
    someBool = true;  //someBools value is unknown
}
else
{
    //Do nothing
}

这可以通过以下方法在单线下实现:

This can be achieved on a one-liner by doing:

someBool = (someStatement) ? true : someBool;

但是我为什么不能做这样的事情:

But why can't I do something like this:

someBool = (someStatement) ? true : ;
//or possibly
someBool = (someStatement) ? true;

这是否有可能?如果是这样,使用一种方法相对于另一种方法有什么好处?如果没有,为什么您不可以呢?

Is this possible in some way? If it is, is there any benefits of using one method over the other? And if not, why shouldn't you be able to do it?

推荐答案

您可以做到:

someBool = (someStatement) ? true : someBool;

我认为这不会使您对以下内容有所了解:

I don't think that gains you a lot of clarity over:

if (someStatement)
{
    someBool = true;
}

但这确实似乎是一种品味问题。我不会说任何一个都明显不好,但是前者并不常见,所以我可能会避免。

But it really seems to be a matter of taste. I wouldn't say either is clearly bad, but the former is uncommon, so I'd probably avoid it.

您问为什么不能使用这样的运算符:

You ask why you can't use the operator like this:

someBool = (someStatement) ? true : ;

这将是一次非常大的语言更改!请记住,分配看起来像这样:

This would be a very big language change! Bear in mind that an assignment looks like this:

<location> = <expression>;

对表达式求值以提供一些值,并将该值存储在location中。 (根据location是变量,属性,字段还是索引表达式,存储操作可能会大不相同。)

The expression is evaluated to give some value, and that value is stored in location. (Depending on whether location is a variable, property, field or indexing expression the "storing" operation could be quite different.)

在这里,您建议右边的表达式除其正常值外,还可以是不变值,它具有特殊的行为,即在赋值语句中使用它时,不会引起存储操作。这不同于任何其他正常值,并且可能令人惊讶。但是,如果在其他地方使用它又意味着什么呢?

Here you're proposing that the value of the expression on the right, in addition to its normal values, can be a "no-change" value, which has the special behaviour that when you use it in an assignment statement it causes no store operation to occur. That's different from any other normal value, and potentially surprising. But what would it mean if you used it in other places?

// Does this call DoSomething when cond is false?
// If so, what value is passed to it?
someObject.DoSomething(cond?x:);

// What happens here if cond is false? Does it cancel
// the entire assignment?
int x = 77 + (cond?2:) * 3 - 4;

// If cond is false, are methods F1 and F2 called or not called?
int x = F1() + (cond?2:) + F2();

// What does this do? Does it skip the return if cond is false?
return (cond?2:);

我认为您很难为用户提出明智,直观和一致的行为运算符在所有这些情况下,除了简单的赋值之外,我认为它不会有用。它只是与其他语言不兼容-包括它会使该语言更难于学习,阅读,理解,实施和解释。简而言之,这是不值得的。

I think you'd find it extremely hard to come up with sensible, intuitive and consistent behaviour for the operator in all these circumstances, and I don't think it would be useful anywhere other than in a simple assignment. It just doesn't fit with the rest of the language - including it would make the language harder to learn, read, understand, implement and explain. It's just not worth it for a tiny bit of conciseness.

这篇关于?没有其他部分的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆