operator Overloading in C#

后端 未结 3 1546
南笙
南笙 2021-01-04 19:24
class Point
{
    private int m_PointX;
    private int m_PointY;

    public Point(int x, int y)
    {
        m_PointX = x;
        m_PointY = y;
    }

    public         


        
3条回答
  •  梦谈多话
    2021-01-04 19:25

    1. Yes. Because you aren't dealing with instances always with the operators.
    2. Just change the types to what you want.

    Here is an example for #2

    public static Point operator+(int value, Point point2)
    {
     // logic here.
    }
    

    You will have to do the other way with the parameters if you want P2 + 2 to work.

    See http://msdn.microsoft.com/en-us/library/8edha89s.aspx for more information.

提交回复
热议问题