How do I write an overload operator where both arguments are interface
Posted
by Eric Girard
on Stack Overflow
See other posts from Stack Overflow
or by Eric Girard
Published on 2009-05-27T16:04:36Z
Indexed on
2010/04/08
3:03 UTC
Read the original article
Hit count: 234
I'm using interface for most of my stuff. I can't find a way to create an overload operator + that would allow me to perform an addition on any objects implementing the IPoint interface
Code
interface IPoint
{
double X { get; set; }
double Y { get; set; }
}
class Point : IPoint
{
double X { get; set; }
double Y { get; set; }
//How and where do I create this operator/extension ???
public static IPoint operator + (IPoint a,IPoint b)
{
return Add(a,b);
}
public static IPoint Add(IPoint a,IPoint b)
{
return new Point { X = a.X + b.X, Y = a.Y + b.Y };
}
}
//Dumb use case :
public class Test
{
IPoint _currentLocation;
public Test(IPoint initialLocation)
{
_currentLocation = intialLocation
}
public MoveOf(IPoint movement)
{
_currentLocation = _currentLocation + intialLocation;
//Much cleaner/user-friendly than _currentLocation = Point.Add(_currentLocation,intialLocation);
}
}
© Stack Overflow or respective owner