Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

Operator Overloading | C#

class Point
    {
        public int x;
        public int y;

        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        public static Point operator + (Point A, Point B) // overloading
        {
            Point point = new Point();

            point.x = A.x + B.x;
            point.y = A.y + B.y;

            return point;
        }
    }
    
class Program{
	private static void Main(string[] args)
    {
    	  Point point1 = new Point(1, 2);
          Point point2 = new Point(2, 3);

          Point point3 = point1 + point2;
          Console.WriteLine($"x: {point3.x} y: {point3.y}"); //prints x: 3 y: 5
          													  
    }


}
 
PREVIOUS NEXT
Tagged: #Operator #Overloading
ADD COMMENT
Topic
Name
2+8 =