Answers for "c# postfix increment operator overload"

C#
0

c# postfix increment operator overload

public static Counter operator ++(Counter c) {
  return new Counter(c.v + 1);
}
// In C# the increment operator must not mutate its argument. Rather it must only compute the incremented value and return it, without producing any side effects. The side effect of mutating the variable will be handled by the compiler.
Counter c1 = new Counter(1);
// Call the object that c1 refers to right now W. W.v is 1.
Counter c2 = c1++;
// This has the semantics of:
  temp = c1
  c1 = operator++(c1) // create object X, set X.v to 2
  c2 = temp
// So c1 now refers to X, and c2 refers to W. W.v is 1 and X.v is 2.
Counter c3 = ++c1;
// This has the semantics of
  temp = operator++(c1) // Create object Y, set Y.v to 3
  c1 = temp
  c3 = temp
// So c1 and c3 now both refer to object Y, and Y.v is 3.
c3++;
// This has the semantics of
  c3 = operator++(c3) // Create object Z, set Z.v to 4
Posted by: Guest on June-17-2020

C# Answers by Framework

Browse Popular Code Answers by Language