How to overload the operator - (minus) in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication1
{
    class Test
    {
        public int _n;

        public static Test operator - (Test x, Test y)
        {
            Test t = new Test();

            t._n = x._n - y._n;

            return t;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Test a = new Test();
            a._n = 10;

            Test b = new Test();
            b._n = 3;

            Test t = a - b;
            Console.WriteLine(t._n);
        }
    }
}

/*
run:
   
7
      
*/

 



answered Mar 16, 2017 by avibootz
edited Mar 16, 2017 by avibootz

Related questions

1 answer 241 views
1 answer 137 views
1 answer 185 views
1 answer 157 views
1 answer 187 views
1 answer 174 views
1 answer 131 views
131 views asked Dec 2, 2022 by avibootz
...