Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 927945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:55:54+00:00 2026-05-15T19:55:54+00:00

I want to overload ++ operator to use pre-increment and post-increment using operator overloading

  • 0

I want to overload ++ operator to use pre-increment and post-increment using operator overloading in my c# class. But only post-increment is working. How to make both function works in my class?
Suppose I made a class ABC like –

using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
    class ABC
    {
      public int a,b;
      public ABC(int x, int y)
      {
        a = x;
        b = y;
      }
      public static ABC operator ++(ABC x)
      {
        x.a++;
        x.b++;
        return x;
      }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ABC a = new ABC(5, 6);
            ABC b, c;
            b = a++;
            Console.WriteLine("After post increment values are {0} and {1} and values of b are {2} and {3}", a.a, a.b, b.a, b.b);// expected output a.a = 6, a.b = 7, b.a = 5, b.b = 6 but not get that
            c = ++a;
            Console.WriteLine("After pre increment values are {0} and {1} and values of c are {2} and {3}", a.a, a.b, c.a, c.b); // expected output a.a = 7, a.b = 7, c.a = 7, c.b = 8 works fine
            Console.Read();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:55:55+00:00Added an answer on May 15, 2026 at 7:55 pm

    Your sample fail to implement this unary operator correctly, as specified in the C# specification in 17.9.1 Unary operators :

    Unlike in C++, this method need not,
    and, in fact, should not, modify the
    value of its operand directly.

    Here is your sample with some micro unit tests :

    using System;
    
    class ABC
    {
      public int a,b;
      public ABC(int x, int y)
      {
        a = x;
        b = y;
      }
    
      public static ABC operator ++(ABC x)
      {
        x.a++;
        x.b++;
        return x;
      }
    }
    
    class Program
    {
        static void Main()
        {
            var a = new ABC(5, 6);
            if ((a.a != 5) || (a.b != 6)) Console.WriteLine(".ctor failed");
    
            var post = a++;
            if ((a.a != 6) || (a.b != 7)) Console.WriteLine("post incrementation failed");
            if ((post.a != 5) || (post.b != 6)) Console.WriteLine("post incrementation result failed");
    
            var pre = ++a;
            if ((a.a != 7) || (a.b != 8)) Console.WriteLine("pre incrementation failed");
            if ((pre.a != 7) || (pre.b != 8)) Console.WriteLine("pre incrementation result failed");
    
            Console.Read();
        }
    }
    

    Your code fail is the post incrementation result and it is due to the fact that you alter the instance of ABC passed as parameter instead of returning a new instance. Corrected code :

    class ABC
    {
      public int a,b;
      public ABC(int x, int y)
      {
        a = x;
        b = y;
      }
    
      public static ABC operator ++(ABC x)
      {
        return new ABC(x.a + 1, x.b + 1);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 492k
  • Answers 492k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Remove the blank space before the opening php tag. May 16, 2026 at 10:19 am
  • Editorial Team
    Editorial Team added an answer var maxPolicies = policyList .GroupBy(p => p.PolicyNumber) .Select(grp => grp.OrderByDescending(p… May 16, 2026 at 10:19 am
  • Editorial Team
    Editorial Team added an answer SELECT Project, (SELECT State + '' FROM table t WHERE… May 16, 2026 at 10:19 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a class that uses a struct, and I want to overload the
I am trying to overload the global operator new and delete for a performance
when I try to overload operator == and != in C#, and override Equal
I have this: typedef string domanin_name; And then, I try to overload the operator<
I have a class that contains decoded video frames. I would like my decoder
Want to display a different message if the time is under a certain amount.
Want to remove all 0 placed at the beginning of some variable. Some options:
I want to write network address to my listview, in a range like 192.168.0.0
I want an option to convert a string to wide string with two different
I want to create a Qt application that takes a random integer and sends

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.