Practical Example

Practical Example

Until now, simple examples have been provided to demonstrate operator overloading and conversion operators. The following code contains a more practical example. Summation notation iterates and totals an expression in a specified range. Figure A-1 offers three examples of summation notation.

Image from book
Figure A-1: Examples of summation notation

Summation is not a predefined type in C#. The following class defines the Summation type. It contains several methods:

  • The Summation class has three constructors. The two- and three-argument constructors delegate to the four-argument constructor and set unspecified parameters a default value.

  • The Calculate method calculates the result of the summation.

  • The class has two operator+ methods, which allow instances of the class to be either lhs or rhs in the binary expression. Each operator+ method extends the iteration by adding to the stop value of the summation. A new instance with an updated number of iterations is returned.

  • The operator++ method extends the number of iterations by one. The current object is then returned.

  • The class has an operator int conversion constructor. This method converts a Summation object into an int value, which is the result of the operation.

  • There are two string functions: ToString returns the result of the operation; ToNotationString displays summation notation and shows the parameters of the operation.

Here is the code for the class:

public class Summation {

    public Summation(int _start, int _stop) :
            this(_start, _stop, 1, 1) {

    public Summation(int _start, int _stop,
        int _product) :
            this(_start, _stop, _product, 1) {

    public Summation(int _start, int _stop,
        int _product, int _power) {

    private void Calculate() {
        int temp;
        for(int count=start;count <= stop;
                ++count) {

            temp=(int)Math.Pow(count, power);

    public static Summation operator+(Summation sum, int val) {
        return new Summation(sum.start, sum.stop+val,
            sum.product, sum.power);

    public static Summation operator+(int val, Summation sum) {
        return new Summation(sum.start, sum.stop+val,
            sum.product, sum.power);

    public static Summation operator++(Summation sum) {
        return sum;

    public static explicit operator int(Summation sum) {
        return sum.propResult;

    private int propResult;
    public int Result {
        get {
            return propResult;

    public override string ToString() {
        return propResult.ToString();

    public string ToNotationString() {
       string line1="\n "+stop.ToString();
        string line2="\n\neeeee";
        string line4="\nee    "+
        string line3="\ne"+new string(' ',line4.Length-2);
        string line5="\ne";
        string line6="\neeeee";
        string line7="\n\n i="+start.ToString();
        return line1+line2+line3+line4+line5+line6+line7;

    private int start;
    private int stop;
    private int product;
    private int power;

The following code tests the Summation type. The ingredients of the summation are read from the command line. In order, provide the start, stop, product, and power for the summation as command-line arguments. The number of iterations is between the start and stop value. In Main, the summation notation is displayed followed by the result. The number of iterations is then increased using the operator++. The new results are then displayed:

    public static void Main(string [] argv){
        Summation sum=
            new Summation(int.Parse(argv[0]),
        Console.WriteLine("\n[Result = {0}]",

        int isum=(int)sum;
        Console.WriteLine("\n\n[Result = {0}]", isum);