简体   繁体   English

c#是否有任何不遵循从左到右评估顺序的陈述?

[英]Does c# have any statements which don't follow left to right evaluation order?

An idle question on language design, see "Does C# have a right hand if like Perl" . 关于语言设计的一个空闲问题,请参阅“如果像Perl一样,C#是否有右手”

For example, in recent C family languages 例如,在最近的C系列语言中

z = foo ( a, b, c );

a is evaluated, then b , then c , so left-to-right. a被评估,然后是b ,然后是c ,所以从左到右。

z = a ? b : c;

a is evaluated, then either b or c , so left-to-right order. a被评估,然后是bc ,所以从左到右的顺序。

In Python, you write a conditional expression as 在Python中,您将条件表达式编写为

z = b if a else c

so the order is the same - a ,then b or c , but it's not left-to-right. 所以顺序是相同的 - a ,然后是bc ,但它不是从左到右。

Strict left-to-right ordering was put into Java to simplify the language; 为了简化语言,在Java中加入了严格的从左到右的排序; ordering is implementation dependent in C or C++. 排序是依赖于C或C ++的实现。

Assignment is (sort of) right to left: 从右到左分配(有点):

int a = b + c;

b+c gets evaluated, then assigned to a. b + c得到评估,然后分配给a。


In general, though, the C# designers have purposely tried to keep most things left->right. 但总的来说,C#设计师故意试图让大多数事情保持正确。 A great example is LINQ. 一个很好的例子是LINQ。 Here, instead of going with the traditional SQL ordering (SELECT XXX FROM XXX), they purposely reordered the query to be more left->right. 在这里,他们没有使用传统的SQL排序(SELECT XXX FROM XXX),而是故意将查询重新排序为更左 - >右。

// Note the left->right ordering:
var results = from member in collection where member.element == condition select member;

// Which is equivelent to:
var resultsNonLinq = collection.Where().Select();

This is part of why I like C# - the consistency in the language is very refreshing, especially when compared to some languages like perl where there is purposely many ways of doing simple things. 这是我喜欢C#的原因之一 - 语言的一致性非常令人耳目一新,特别是与某些语言(如perl)相比,其中有许多方法可以做简单的事情。

不,它不会检查MS C#lang规范或ECMA334规范(第14章)

no, but you're obviously looking for something. 不,但你显然在找东西。

Try this 试试这个

C# has the ternary operator: C#有三元运算符:

result = a == b ? c : d

is equivalent to 相当于

if (a == b)
    result = c;
else
    result = d;

It's not strictly right to left, but it does reduce the amount of needed code. 它不是严格的向右,但它确实减少了所需的代码量。

You forgot to put any actual question in your question... I assume that you mean the title to be the question... ;) 你忘了在你的问题中提出任何实际问题......我认为你的意思是标题是问题......;)

You can use an extension and a delegate to make something that looks like a right-to-left evaluation. 您可以使用扩展名和委托来制作看似从右到左评估的内容。 Actually it's still evaluated from left to right, but the code to the left is put in a delegate and only used based on the result of the condition on the right. 实际上它仍然是从左到右进行评估,但是左边的代码放在一个委托中,只根据右边条件的结果使用。

You can make an extension to the Action delegate with the name If. 您可以使用名称If对Action委托进行扩展。

public static class PostCondition {
   public static void If(this Action action, bool condition) {
      if (condition) action();
   }
}

Now you can make a delegate, cast it to Action, and call the If method on it: 现在,您可以创建一个委托,将其强制转换为Action,并在其上调用If方法:

((Action)(() => Console.WriteLine("Yes"))).If(true);

It get's a bit cleaner if you divide it into two statements: 如果将它分成两个语句,它会变得更清晰:

Action writeYes = () => Console.WriteLine("Yes");
writeYes.If(true);

Still, it's not really an improvement over the regular if statement anyway... 不过,无论如何,它并不比普通的if语句真正改进......

I assume you're actually referring to operators that are right-associative, which are operators that act on the right side of the operators. 我假设你实际上指的是右关联运算符,它们是运算符右侧的运算符。

Most operators in C# are left-associative, except for: C#中的大多数运算符都是左关联的,除了:

  • The assignment operator (x = expression) 赋值运算符(x =表达式)
  • All unary operators ("-" , "!", "~"), including type casting (but that's not really an operator) 所有一元运算符 (“ - ”,“!”,“〜”),包括类型转换(但不是真正的运算符)

Technically, there's another one: the ternary operator (a ? b : c), but that's such a different beast that it hardly qualifies as a right-associative operator. 从技术上讲,还有另外一个: 三元运算符 (a?b:c),但这是一个如此不同的野兽,它几乎没有资格作为右关联运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM