Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C# what is the difference between myInt++ and ++myInt?

Tags:

syntax

c#

I'm having a hard time understanding what the difference is between incrementing a variable in C# this way:

myInt++;

and

++myInt;

When would ever matter which one you use?

I'll give voteCount++ for the best answer. Or should I give it ++voteCount...

like image 336
Micah Avatar asked Jan 12 '09 21:01

Micah


People also ask

What does |= mean in C?

The bitwise OR assignment operator ( |= ) uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable.

What is ?: operator in C?

C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.

What does %d do in C?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

What is -= in C?

This operator is a combination of '-' and '=' operators. This operator first subtracts the value on the right from the current value of the variable on left and then assigns the result to the variable on the left. Example: (a -= b) can be written as (a = a - b) If initially value stored in a is 8.


2 Answers

There is no difference when written on its own (as shown) - in both cases myInt will be incremented by 1.

But there is a difference when you use it in an expression, e.g. something like this:

MyFunction(++myInt);
MyFunction(myInt++);

In the first case, myInt is incremented and the new/incremented value is passed to MyFunction(). In the second case, the old value of myInt is passed to MyFunction() (but myInt is still incremented before the function is called).

Another example is this:

int myInt = 1;
int a = ++myInt;
// myInt is incremented by one and then assigned to a.
// Both myInt and a are now 2.
int b = myInt++;
// myInt is assigned to b and then incremented by one.
// b is now 2, myInt is now 3

BTW: as Don pointed out in a comment the same rules are also valid for decrement operations, and the correct terminology for these operations are:

++i; // pre-increment
i++; // post-increment
--i; // pre-decrement
i--; // post-decrement

As Jon Skeet points out:

Others have shown where it makes a difference, and have commented that as a single statement it doesn't make a difference.

I'd like to add that it's almost always a bad idea to use it where it makes a difference. I suspect there may be some times where it's more readable to have code such as:

Console.WriteLine("Foo: {0}", foo++);

than:

Console.WriteLine("Foo: {0}", foo);
foo++; 

... but they're very rare! The latter of these two samples makes the ordering crystal clear immediately - the former requires a bit of thinking (to my poor brain, anyway). Think of the readability first.

like image 188
10 revs, 4 users 55% Avatar answered Sep 24 '22 01:09

10 revs, 4 users 55%


Others have shown where it makes a difference, and have commented that as a single statement it doesn't make a difference.

I'd like to add that it's almost always a bad idea to use it where it makes a difference. I suspect there may be some times where it's more readable to have code such as:

Console.WriteLine("Foo: {0}", foo++);

than:

Console.WriteLine("Foo: {0}", foo);
foo++;

... but they're very rare! The latter of these two samples makes the ordering crystal clear immediately - the former requires a bit of thinking (to my poor brain, anyway). Think of the readability first.

like image 22
Jon Skeet Avatar answered Sep 22 '22 01:09

Jon Skeet