Overload operator for struct - CSharp Custom Type

CSharp examples for Custom Type:struct

Description

Overload operator for struct

Demo Code

using static System.Console;
using System;/*from   w w  w.j  av  a2 s.  c  om*/
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading;
class Program
{
   static void Main(string[] args)
   {
      var dv1 = new DisplacementVector(3, 5);
      var dv2 = new DisplacementVector(-2, 7);
      var dv3 = dv1 + dv2;
      WriteLine($"({dv1.X}, {dv1.Y}) + ({dv2.X}, {dv2.Y}) = ({dv3.X}, {dv3.Y})");
   }
}
public struct DisplacementVector
{
   public int X;
   public int Y;
   public DisplacementVector(int initialX, int initialY){
      X = initialX;
      Y = initialY;
   }
   public static DisplacementVector operator +(DisplacementVector vector1,DisplacementVector vector2)
   {
      return new DisplacementVector(vector1.X + vector2.X,vector1.Y + vector2.Y);
   }
}

Result


Related Tutorials