Overloading of Post and Pre Increment Operators Using Classes in C# (Sharp)

The following code is the example of post and pre increment operators using classes :
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication15
{
    class numbercount
    {
        public int i;
        public numbercount(int initval)
        {
            this.i = initval;
        }
        public static numbercount operator ++(numbercount arg)
        {
            return new numbercount(arg.i + 1);
        }
    }
    class testclass
    {
        static void Main(string[] args)
        {
            numbercount count1 = new numbercount(1);
            numbercount count2 = count1++;
            Console.WriteLine(count1.i);
            Console.WriteLine(count2.i);
            count2 = ++count1;
            Console.WriteLine(count1.i);
            Console.WriteLine(count2.i);
            Console.ReadLine();
        }
    }
}

Add Comment