How to overload the operator / (division) in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication1
{
    class Test
    {
        public float _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:
   
3.333333
      
*/

 



answered Mar 16, 2017 by avibootz

Related questions

1 answer 146 views
1 answer 169 views
1 answer 201 views
201 views asked Mar 16, 2017 by avibootz
1 answer 202 views
1 answer 183 views
1 answer 135 views
135 views asked Dec 2, 2022 by avibootz
...