Why does >= work but => not?

人盡茶涼 提交于 2020-06-27 13:57:46

问题


When checking if a integer is the same or above a current number.. so I type

if (5 => 6) { //Bla } 

but it shows this as a error. Why? Isn't it exactly the same as

if (5 >= 6) { //Bla } 

回答1:


The reason why it does not work is because => is not equivalent to >=.

=> is used in a lambda expression. Like :

(int x, string s) => s.Length > x

I do agree it is annoying. Before lambda expressions I used to get it wrong sometimes. Now I always know that one (=>) is a lambda expression and and other (>=) is the greater than equal to sign




回答2:


Because the operator is >= not =>.

The writers of the language could have chosen either syntax, but had to choose one. Having two operators meaning the same thing would be confusing at best.

However, the operator is read "greater than or equal to" so it does make sense that the > symbol is first.

Also => is now used for Lambda expressions.




回答3:


Because => is meant for lambda expressions:

Action<object> print = o => Console.WriteLine(o);
print(123);

Besides, you don't say "equal to or greater than", which is what => would have been pronounced as otherwise.




回答4:


The confusion here is that you're assuming >= is two operators smooshed together. In fact, it's only one operator with two characters, much the same as tons of other operators (+=, *=, -=, etc).




回答5:


Why should it be? =! is not the same as != either. This is a part of the languages syntax.

In this specific case, => is also used for lambda expressions so it has another purpose.




回答6:


Because it is called greater or equal to. Not equal or greater than. Simple uh?




回答7:


In C# the greater than or less than sign must come BEFORE the equal sign. It is just part of the syntax of the language.




回答8:


Because => stands for Lambda expressions in c#.

>= stands for greater than or equal to, as you already know.

The syntax is such that you have to use >= while comparing two entities. Also just additionally you can notice that even a space between them will give errors - > =




回答9:


No, it is not this same. Correct operator in c# is >= for comparission and => for lambda expression.




回答10:


@Barry's answer is probably the most insightful of the lot here. A single operator does not mean a single character; the fact that > and = combine to form >= does not mean that it's doing both > and =; it's doing a single operation. The fact that the defined operator for that operation includes the characters for two other similar operations is irrelevent.

I suppose if you really wanted to you could override it so that both >= and => worked the same way -- C# does allow operator overrides. But it would be a bad idea because as others have already said, => is actually in use for other purposes.



来源:https://stackoverflow.com/questions/5648771/why-does-work-but-not

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!