C #의 사전 및 사후 증가
C # 컴파일러가 사전 및 사후 증가 및 감소를 처리하는 방법에 대해 약간 혼란 스럽습니다.
다음을 코딩 할 때 :
int x = 4;
x = x++ + ++x;
x나중에 값 10을 갖게됩니다. 나는이 생각하는 사전 증가 세트 때문에 x에 5그것을 만들고, 5+5어떤 평가됩니다에 10. 그런 다음 사후 증가가 업데이트됩니다 x에 6,하지만 때문에이 값은 사용되지 않습니다 10에 할당됩니다 x.
하지만 내가 코딩 할 때 :
int x = 4;
x = x-- - --x;
그런 다음 나중에 x될 것 2입니다. 왜 이것이 사실인지 설명 할 수 있습니까?
x--4가 될 것이지만 순간에 3이 될 --x것이므로 2로 끝날 것입니다.
x = 4 - 2
btw, 첫 번째 경우는 x = 4 + 6
다음은 각 부분의 값을 출력하는 작은 예입니다. 이렇게하면 더 잘 이해할 수 있습니다.
static void Main(string[] args)
{
int x = 4;
Console.WriteLine("x++: {0}", x++); //after this statement x = 5
Console.WriteLine("++x: {0}", ++x);
int y = 4;
Console.WriteLine("y--: {0}", y--); //after this statement y = 3
Console.WriteLine("--y: {0}", --y);
Console.ReadKey();
}
이것은 인쇄
x++: 4
++x: 6
y--: 4
--y: 2
해당 명령문에서 생성 된 IL을 살펴 보겠습니다.
IL_0002: ldloc.0
x 값을 스택에로드합니다. 스택 => (4)
IL_0003: dup
스택의 최상위 항목을 복제합니다. 스택 => (4, 4)
IL_0004: ldc.i4.1
스택에 1을 밀어 넣습니다. 스택 => (1, 4, 4)
IL_0005: sub
두 개의 상위 값을 빼고 결과를 스택에 푸시합니다. 스택 => (3, 4)
IL_0006: stloc.0
스택의 최상위 값을 x에 다시 저장합니다. 스택 => (4)
IL_0007: ldloc.0
x 값을 스택에 다시로드합니다. 스택 => (3, 4)
IL_0008: ldc.i4.1
값 1을 스택에로드합니다. 스택 => (1, 3, 4)
IL_0009: sub
둘을 뺍니다. 스택 => (2, 4)
IL_000A: dup
상단 값 복제 => (2, 2, 4)
IL_000B: stloc.0
상위 값을 다시 x에 저장합니다. 스택 => (2, 4)
IL_000C: sub
두 개의 상위 값을 뺍니다. 스택 => (2)
IL_000D: stloc.0
이 값을 x에 다시 저장하십시오. x == 2
귀하의 의견에서 :
I thought that post- and pre-increments are executed after / before evaluation of the complete codeline - but they are executed after / before the evaluation of each item in the expression.
Your misunderstanding is an extremely common one. Note that in some languages, like C, it is not specified when the side effect becomes visible and it is therefore legal, but not required, for your statement to be true in C.
This is not the case in C#; in C# side effects of code on the left side of an expression are always observed to happen before code on the right side executes (from a single thread; in multithreaded scenarios all bets are off.)
For a more detailed explanation of what the increment operators do in C#, see:
What is the difference between i++ and ++i?
There are a great many additional links there to articles I've written on this often-misunderstood topic.
The most interesting thing that you'll get a completely different answer with C++.Net compiler.
int x = 4;
x = x++ + ++x; // x = 11
x = 4;
x = x-- - --x; // x = -1
Of course the difference in results is determined by different semantics - it seems normal. But despite the understanding the fact that two .net compilers don't behave in a similar manner for such basic things confuses me too.
In this example,
int x = 4;
x = x++ + ++x;
you can break it like:
x = 4++; which is = 5
x = 4 + ++5; which is 4 + 6
x = 10
Similarly,
int x = 4;
x = x-- - --x;
Here,
x = 4--; which is = 3
x = 4 - --3; which is 4 - 2
x = 2
Simply putting you can say, replace the current value of x, but for every ++ or -- add/subtract a value from x.
I think the explanation for the ++ + ++ case is wrong:
command...........value of x
..................undefined
int x=4 ..........4
x++...............5 (first summand is 4)
++x...............6 (second summand is 6)
x=summand1+summand2 ..4+6=10
Analogous the explanation for the -- - -- case is
command...........value of x
..................undefined
int x=4 ..........4
x--...............3 (subtactor is 4)
--x...............2 (subtrahend is 2)
x=subtractor-subtrahend ..4-2=10
참고URL : https://stackoverflow.com/questions/8573190/pre-post-increment-in-c-sharp
'IT Share you' 카테고리의 다른 글
| 내 Haskell 프로그램이나 라이브러리가 버전 번호를 어떻게 찾을 수 있습니까? (0) | 2020.12.01 |
|---|---|
| 패턴 매칭과 가드의 차이점은 무엇입니까? (0) | 2020.12.01 |
| TPL과 비동기 / 대기 (스레드 처리)의 차이점 (0) | 2020.12.01 |
| location.href 속성 대 location.assign () 메서드 (0) | 2020.12.01 |
| 그놈 쉘 확장을 작성하기위한 문서 (0) | 2020.12.01 |