CSharp examples for Custom Type:Operator Overloading
Overloading the Increment and Decrement Operators
using System;/*ww w . j av a2 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 operator++ ( MyCharacter orig ) { MyCharacter result = new MyCharacter(); result.ch = (char)(orig.ch + 1); return result; } static public MyCharacter operator-- ( MyCharacter orig ) { MyCharacter result = new MyCharacter(); result.ch = (char)(orig.ch - 1); return result; } } public class myAppClass { public static void Main(String[] args) { MyCharacter aaa = new MyCharacter('g'); MyCharacter bbb = new MyCharacter('g'); Console.WriteLine("Original value: {0}, {1}", aaa.ch, bbb.ch); aaa = ++aaa; bbb = --bbb; Console.WriteLine("Current values: {0}, {1}", aaa.ch, bbb.ch); aaa = ++aaa; bbb = --bbb; Console.WriteLine("Final values: {0}, {1}", aaa.ch, bbb.ch); } }