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 }
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
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.
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.
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).
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.
Because it is called greater or equal to. Not equal or greater than. Simple uh?
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.
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 - > =
No, it is not this same. Correct operator in c# is >= for comparission and => for lambda expression.
@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.
精彩评论