Look at this thread: http://www.daniweb.com/forums/thread206859.html
In C# this would give:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            i = i++;
            Console.WriteLine(i);
            Console.ReadKey();
        }
    }
}

Also in C# it prints 0. I wonder why. I think Java is correct here. Although it is postincrement, this postincrement must happen before the WriteLine. Or am I totally wrong here?

Recommended Answers

All 7 Replies

I always though it was executed after the entire expression, however if you run this code:

class Program
{
    static void Main(string[] args)
    {
        int i = 0;
        int d = (i++) + (i++);
        Console.WriteLine(d);
        Console.ReadKey();
    }
}

It produces 1. Therefore the postincrement must be evaluated directly after the value beforehand is used in the expression. So for your example, it would:
- Evaluate i++ as 0.
- Increase i by 1 (i=1).
- Set i to 0.
C or C++, on the other hand, might depend on the implementation.

Thanks for the response guys.
But I considered i++; to be the same as i = i + 1;
So I did this and it prints what I expected, but this would mean that i=i+1; is not equivalent to i++; Right? or Wrong?

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            //i = i++;  //this prints 0
            i = i = i + 1; // this prints 1
            Console.WriteLine(i);
            Console.ReadKey();
        }
    }
}

i = i + 1 would be equivalent to ++i. Since the expression (i = i + 1) evaluates as i + 1.

Thanks for the replies guys.
It is becoming cristal clear to me now.
At last ;)

Final note :
It's of course a bit strange to assign a postincrement to something.
You normally would use it as a standalone like i++; or in a for loop or so, with no problems.

Danny,
In .net framework , types system falls into two categories:
1. Value types (simple types (int,char etc - they are also struct),struct & enum)
2. Reference types - (classes,interfaces, arrays, & delegate)
Hence an output with ++ post/pre is understood by the following fact:
The assignment to a varible of a value type creates a copy of the assigned value, while the assignment to a variable of a reference type creates a copy of the reference but not of the referenced object.

Example - value type overload ++ operator

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
struct Test
{
    public  int n;
    public Test(int n) { this.n = n; }
    public static  Test  operator ++(Test t)
     {
            t.n=1000; // you may use any value
            Test h = new Test(t.n);
            return h;
     }
}
 
class Program
    {
        static void Main(string[] args)
        {
            Test p=new Test(10);
            p = p++;
            Console.WriteLine(p.n);
        }
    }

Example - Reference type

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Test
{
    public  int n;
    public Test(int n) { this.n = n; }
    public static  Test  operator ++(Test t)
     {
            t.n=1000; // you may use any value
            Test h = new Test(t.n);
            return h;
     }
}
class Program
    {
        static void Main(string[] args)
        {
            Test p=new Test(10);
            p = p++;
            Console.WriteLine(p.n);
        }
    }
commented: You are great man! I appreciate this very much! +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.