有人可以解释一下吗?操作员?

发布于 2024-09-16 15:26:34 字数 386 浏览 3 评论 0原文

可能的重复:
C#:i++ 和 ++i 之间有什么区别?< /a>

我经常看到这个运算符 (++)。我知道它最终会做什么,但似乎有一些我不明白的规则。例如,将它放在要使用它的变量之前或之后似乎很重要。有人可以解释一下吗?

Possible Duplicate:
C#: what is the difference between i++ and ++i?

I see this operator (++) very often. I know what it does ultimately, but it seems like there's some rules I don't understand. For example, it seems to matter if you put it before or after the variable you're using it on. Can someone explain this?

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

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

发布评论

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

评论(6

ゞ记忆︶ㄣ 2024-09-23 15:26:53

如果将 ++ 运算符放在变量之前,则变量首先递增。如果将 ++ 运算符放在变量后面,则变量之后会递增。

例如(C#):

int x = 0;
Console.WriteLine("x 是 {0}", x++); // 打印 x is 0

int y = 0;
Console.WriteLine("y 是 {0}", ++y); // 打印 y 为 1

If you put the ++ operator before the variable, it is incremented first. If you put the ++ operator after the variable, it is incremented after.

For example(C#):

int x = 0;
Console.WriteLine("x is {0}", x++); // Prints x is 0

int y = 0;
Console.WriteLine("y is {0}", ++y); // Prints y is 1

ゝ杯具 2024-09-23 15:26:52

另一种看待它的方式...这里有两个函数,其作用与 prefix/postfix ++ 相同。这说明前缀在理论上更快,因为不需要临时变量来存储“前一个”值:

// same as x ++;
int PostfixIncrement(ref int x)
{
    int y = x;
    x = x + 1;
    return y;
}

// same as ++ x;
int PrefixIncrement(ref int x)
{
    x = x + 1;
    return x;
}

Another way to see it... here are two functions that do the same as prefix/postfix ++. This illustrates that prefix is, in theory, faster, because no temporary variable is needed to store the "previous" value:

// same as x ++;
int PostfixIncrement(ref int x)
{
    int y = x;
    x = x + 1;
    return y;
}

// same as ++ x;
int PrefixIncrement(ref int x)
{
    x = x + 1;
    return x;
}
半透明的墙 2024-09-23 15:26:50

好吧,如果你这样说它

variable++

首先使用变量并递增它(+1)
另一方面,如果您

++variable

首先递增变量然后使用它

well if you put it like

variable++

It first uses the variable and the increments it (+1)
On the otherhand if you

++variable

It first increments the variable and then uses it

橘和柠 2024-09-23 15:26:48

如果将 ++ 运算符放在变量之前,则变量首先递增。
如果将 ++ 运算符放在变量后面,则变量会在后面递增。

例如(C#):

int x = 0;
Console.WriteLine("x is {0}", x++); // Prints x is 0

int y = 0;
Console.WriteLine("y is {0}", ++y); // Prints y is 1

希望这能解决问题。

If you put the ++ operator before the variable, it is incremented first.
If you put the ++ operator after the variable, it is incremented after.

For example(C#):

int x = 0;
Console.WriteLine("x is {0}", x++); // Prints x is 0

int y = 0;
Console.WriteLine("y is {0}", ++y); // Prints y is 1

Hope this cleared it up.

黑凤梨 2024-09-23 15:26:46

http://msdn.microsoft.com/en -us/library/36x43w8w(v=VS.80).aspx

增量运算符 (++) 将其操作数增加 1。增量运算符可以出现在其操作数之前或之后:

第一种形式是前缀增量手术。运算结果是操作数递增后的值。

第二种形式是后缀增量运算。运算的结果是操作数递增之前的值。

http://msdn.microsoft.com/en-us/library/36x43w8w(v=VS.80).aspx

The increment operator (++) increments its operand by 1. The increment operator can appear before or after its operand:

The first form is a prefix increment operation. The result of the operation is the value of the operand after it has been incremented.

The second form is a postfix increment operation. The result of the operation is the value of the operand before it has been incremented.

蹲在坟头点根烟 2024-09-23 15:26:43

该语句

x++;

完全相同,

x = x + 1;

除了,x 仅计算一次(如果它是涉及属性获取器的表达式,则情况会有所不同)。

以下两者之间的区别

DoSomething(x++);   // notice x first, then ++
DoSomething(++x);   // notice ++ first, then x

在于:在第一个中,方法 DoSomething 将看到 x前一个之前< /em> 它被增加了。在第二个中,它将看到(增量)值。

有关详细信息,请参阅 C# 运算符在 MSDN 上。

可以为您自己的类声明自定义 ++ 运算符,在这种情况下,该运算符可以执行不同的操作。如果要定义自己的 ++ 运算符,请参阅 运算符重载教程

The statement

x++;

is exactly equivalent to

x = x + 1;

except that x is evaluated only once (which makes a difference if it is an expression involving property getters).

The difference between the following two:

DoSomething(x++);   // notice x first, then ++
DoSomething(++x);   // notice ++ first, then x

Is that in the first one, the method DoSomething will see the previous value of x before it was incremented. In the second one, it will see the new (incremented) value.

For more information, see C# Operators on MSDN.

It is possible to declare a custom ++ operator for your own classes, in which case the operator can do something different. If you want to define your own ++ operator, see Operator Overloading Tutorial on MSDN.

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