Operators for Mathematical Functions - CSharp Custom Type

CSharp examples for Custom Type:Operator Overloading

Description

Operators for Mathematical Functions

Demo Code

using System;/*  ww  w  . ja va2 s  . c  o  m*/
public class MyCharacter
{
   private char private_ch;
   public MyCharacter() { this.ch = ' '; }
   public MyCharacter(char val) { this.ch = val; }
   public char ch
   {
      get{ return this.private_ch; }
      set{ this.private_ch = value; }
   }
   static public MyCharacter Add ( MyCharacter orig, int val )
   {
      MyCharacter result = new MyCharacter();
      result.ch = (char)(orig.ch + val);
      return result;
   }
   static public MyCharacter Subtract ( MyCharacter orig, int val )
   {
      MyCharacter result = new MyCharacter();
      result.ch = (char)(orig.ch - val);
      return result;
   }
}
public class myAppClass
{
   public static void Main(String[] args)
   {
      MyCharacter aaa = new MyCharacter('a');
      MyCharacter bbb = new MyCharacter('b');
      Console.WriteLine("Original value: {0}, {1}", aaa.ch, bbb.ch);
      aaa = MyCharacter.Add( aaa, 3 );
      bbb = MyCharacter.Subtract( bbb, 1 );
      Console.WriteLine("Final values: {0}, {1}", aaa.ch, bbb.ch);
   }
}

Result


Related Tutorials