为什么 >= 有效但 =>不是?

发布于 2024-10-31 17:02:37 字数 170 浏览 1 评论 0原文

当检查一个整数是否等于或大于当前数字时..所以我输入,

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

但它显示这是一个错误。为什么?这不是完全一样吗

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

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 } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

浮华 2024-11-07 17:02:37

它不起作用的原因是 => 不等于 >=

=> 用于 lambda 表达式。喜欢:

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

我确实同意这很烦人。在使用 lambda 表达式之前,我有时会出错。现在我总是知道一个 (=>) 是一个 lambda 表达式,另一个 (>=) 是更大的表达式大于等于

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

旧伤慢歌 2024-11-07 17:02:37

因为运算符是 >= 而不是 =>

该语言的编写者可以选择任何一种语法,但必须选择其中一种。如果两个运算符表示相同的意思,那么充其量只会造成混乱。

然而,该运算符被读作“大于或等于”,因此 > 符号在前面确实有意义。

此外,=> 现在也用于 Lambda 表达式。

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.

旧时模样 2024-11-07 17:02:37

因为 => 适用于 lambda 表达式:

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

此外,您不会说“等于或大于”,这就是 => 的发音否则。

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.

冷清清 2024-11-07 17:02:37

这里的困惑在于您假设 >= 是两个运算符混合在一起。事实上,它只是一个带有两个字符的运算符,与大量其他运算符(+=、*=、-= 等)非常相似。

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).

鹤仙姿 2024-11-07 17:02:37

为什么会这样呢? =!也与 != 不同。这是语言语法的一部分。

在这种特定情况下, =>也用于 lambda 表达式,因此它还有另一个用途。

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.

一生独一 2024-11-07 17:02:37

因为它被称为大于或等于。不等于或大于。简单呃?

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

∞琼窗梦回ˉ 2024-11-07 17:02:37

在 C# 中,大于号或小于号必须位于等号之前。它只是该语言语法的一部分。

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.

南巷近海 2024-11-07 17:02:37

因为=>代表c#中的Lambda表达式。

正如您所知,>= 代表大于或等于。

语法是这样的,在比较两个实体时必须使用 >= 。此外,您还可以注意到,即使它们之间有空格也会出错 - > =

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 - > =

诠释孤独 2024-11-07 17:02:37

不,这不一样。 C# 中正确的运算符是用于比较的 >= 和 =>对于 lambda 表达式。

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

甜警司 2024-11-07 17:02:37

@Barry 的答案可能是这里最有洞察力的。单个运算符并不意味着单个字符; >= 组合形成 >= 的事实并不意味着它同时执行 >=;它正在执行单个操作。该操作的定义运算符包括其他两个类似操作的字符这一事实是无关紧要的。

我想如果您真的想要的话,您可以覆盖它,以便 >==> 以相同的方式工作 - C# 就是这样允许操作员覆盖。但这是一个坏主意,因为正如其他人已经说过的那样, => 实际上用于其他目的。

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文