I want to overload ++
operator to use pre-increment and post-increment using operator overloading in my c# class. But only post-increment is working. How to make both function works in my class?
Suppose I made a class ABC like -
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
class ABC
{
public int a,b;
public ABC(int x, int y)
{
a = x;
b = y;
}
public static ABC operator ++(ABC x)
{
x.a++;
x.b++;
return x;
}
}
class Program
{
static void Main(string[] args)
{
ABC a = new ABC(5, 6);
ABC b, c;
b = a++;
Console.WriteLine("After post increment values are {0} and {1} and values of b are {2} and {3}", a.a, a.b, b.a, b.b);// expected output a.a = 6, a.b = 7, b.a = 5, b.b = 6 but not get that
c = ++a;
Console.WriteLine("After pre increment values are {0} and {1} and values of c are {2} and {3}", a.a, a.b, c.a, c.b); // expected output a.a = 7, a.b = 7, c.a = 7, c.b = 8 works fine
Console.Read();
}
}
}