Below is the code to overload "minus" operator in C#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace oproverloadDemo
{
class A
{
int x;
A()
{
x = 0;
}
A(int x)
{
this.x = x;
}
public static A operator+(A a1, A a2)
{
A a3 = new A();
a3.x = a1.x + a2.x;
return a3;
}
void show()
{
Console.WriteLine("x={0}", x);
}
static void Main(string[] args)
{
A a = new A(10);
A b = new A(20);
A c = a + b;
c.show();
Console.ReadLine();
}
}
}
Comments
Post a Comment