简体   繁体   English

C#中的一元增量运算符i ++

[英]unary increment operator i++ in C#

In my last interview i found this tricky question. 在上一次采访中,我发现了这个棘手的问题。 after these two lines what will be the value of i. 在这两行之后,i的值是多少。

int i=c;
//c is a constant
i=i++;

where c is a constant(where c is initialised before). 其中c是常数(其中c之前已初始化)。 please give me step by step answer rather than one word answer. 请给我逐步的答案,而不是一个字的答案。

finally value of i remains same as c. 最终,i的值与c相同。

Explanation:- when i is assigned with i++ ; 说明:-当我分配给i ++时

step 1. first i++ return value c(but not assigned to left-hand operand i). 步骤1.第一个i ++返回值c(但未分配给左操作数i)。

step 2. then i++ increments value of i to c+1. 步骤2。然后,i ++将i的值增加到c + 1。

step 3. i is assigned with value c that is returned in step1. 步骤3。为i分配了在步骤1中返回的值c。

In effect value of i remains same but some where along the execution it was c+1, But finally it is assigned with value c. 实际上,i的值保持不变,但在执行过程中某些地方为c + 1,但最终它被赋值为c。

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

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