我想重载++
运算符在我的c#类中使用运算符重载使用前增量和后增量。但只有后增加工作。如何使这两个功能在我的课堂上工作? 假如我做了一个ABC类像 -如何在我的课堂上实现前后增量/减量运算符?
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();
}
}
}
你可以发布你的代码为你的前后增量,所以我们可以看到是否有问题? – JLWarlow 2010-07-19 15:46:19
向我们显示您的代码。 – dthorpe 2010-07-19 15:46:29
请现在回答。我已经上传了我的代码。 – chanchal1987 2010-07-19 16:20:38