Click here to Skip to main content
15,887,135 members
Articles / Programming Languages / C#

Arithmetic in Generic Classes in C#

Rate me:
Please Sign up or sign in to vote.
4.65/5 (11 votes)
23 Feb 2009CC (ASA 2.5)8 min read 72.5K   556   27  
A discussion of doing arithmetic in generic classes and a small utility to make it easy.
using System;
using System.Collections.Generic;
using System.Text;
using MiscUtil;

namespace Calculator {

    /// <summary>
    /// Since MiscUtil doesn't include operators, I added this class to make it work.
    /// This type is used by in generic classes to take the place 
    /// of variables of unknown type.
    /// It also defines operators that can be used instead of method names.
    /// </summary>
    /// <typeparam name="T">Type that we will be doing arithmetic with</typeparam>
    public class MiscUtilNumber<T> {
		private T value;

		private MiscUtilNumber(T value) {
			this.value = value;
		}

        //IMPORTANT:  The implicit operators allows an object of type Number<T> to be
        //easily and seamlessly wrap an object of type T. 
		public static implicit operator MiscUtilNumber<T>(T a) {
			return new MiscUtilNumber<T>(a);
		}

        //IMPORTANT:  The implicit operators allows an object of type Number<T> to be
        //easily and seamlessly wrap an object of type T. 
        public static implicit operator T(MiscUtilNumber<T> a) {
			return a.value;
		}

		public static MiscUtilNumber<T> operator +(MiscUtilNumber<T> a, MiscUtilNumber<T> b) {
            return Operator.Add(a.value, b.value);
		}

        public static MiscUtilNumber<T> operator -(MiscUtilNumber<T> a, MiscUtilNumber<T> b) {
			return Operator.Subtract(a.value, b.value);
		}

		public static bool operator >(MiscUtilNumber<T> a, MiscUtilNumber<T> b) {
            return Operator.GreaterThan(a.value, b.value);
		}

		public static bool operator <(MiscUtilNumber<T> a, MiscUtilNumber<T> b) {
            return Operator.LessThan(a.value, b.value);
		}

        public static MiscUtilNumber<T> operator *(MiscUtilNumber<T> a, MiscUtilNumber<T> b) {
            return Operator.Multiply(a.value, b.value);
        }

        public static MiscUtilNumber<T> operator /(MiscUtilNumber<T> a, MiscUtilNumber<T> b){
            return Operator.Divide(a.value, b.value);
        }

        public static MiscUtilNumber<T> operator /(MiscUtilNumber<T> a, int b) {
            return Operator.DivideInt32(a.value, b); ;
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
Software Developer (Senior) Coleman Insights
United States United States
Bill Fugina

Bill is a software developer at Coleman Insights.
He has been working professionally with C# and .Net since Visual Studio 2003 and for fun since the public beta. Before .Net, Bill worked with Delphi and Object Pascal.

Bill blogs at http://www.dogspots.com

Comments and Discussions