I use the interface for most of my stuff. I can’t find a way to create an overload + operator that will allow me to add for any objects that implement 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); } }
No. Imagine if you have two instances of IPoint, a and b, both different classes (which implement IPoint). Now call "a + b". Which operator + is called? What specific type is returned?
: , : " #, ". , , , .
, , , . , .Add movement Point Type . , , #, , .
.Add
movement
Point Type
, , Point Type MoveOf, Point , , , Point .
MoveOf
Point
, , , Point Type MoveOf, movement MoveOf . , , all, IPoint MoveOf.
IPoint
Source: https://habr.com/ru/post/1709212/More articles:How can I correctly define my build platform? - cDecimal value Check for zero - mathInstrumental (diagnostic) library for C ++ - c ++Стиль конкретных дат в jquery ui datepicker плагин - jquery-uihttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1709211/acegispring-security-grails-plug-in-not-seeing-changes-to-a-user-instance&usg=ALkJrhh8YiDVronUvfNzyJJtUVYvKUwsFwWhy does this equality test fail? - c #General methods in non-generic types - genericsУстановка Window.Content не разрушает пользовательский интерфейс, который был там раньше? - c#WatiN Error: Unable to Download - watinHow to automatically rename the Application Files folder in a ClickOnce deployment - visual-studioAll Articles