Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "count++" return in C#?

Tags:

c#

Just ran into a bit of code that wasn't doing what I thought it should. Do other people think this should return 1? Is there a good explanation as to why it doesn't??

int count = 0;

count++.ToString(); // Returns 1 no?

I always thought count++ was the same as count = count + 1...

like image 494
swingdoctor Avatar asked Feb 17 '11 15:02

swingdoctor


People also ask

What does count function do in C?

The count() function is available in the algorithm. h header file in C++. This function is used to get the number of appearances of an element in the specified range.

Is count ++ the same as count += 1?

In programming count++ is equivalent to count+1 , then why is the difference in above two examples. I know its something related to closure property and hoisting.

What does count ++ do in C?

count++ is post increment where ++count is pre increment. suppose you write count++ means value increase after execute this statement. but in case ++count value will increase while executing this line.

What is count variable in C programming?

The C supports ellipsis. This is used to take variable number of arguments to a function. User can count the arguments by using one of the three different ways. By passing the first argument as count of the parameters. By passing last argument as NULL.


1 Answers

x++ is a post increment operator. It means that the value of x is incremented, but the old (non-incremented) value of x is returned (0 in your case, to which ToString is applied).

To get the behavior you want, use the pre increment operator ++x.

like image 175
Heinzi Avatar answered Oct 14 '22 07:10

Heinzi