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

  • SEARCH
  • Home
  • 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 8871477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:00:03+00:00 2026-06-14T18:00:03+00:00

Possible Duplicate: Post-increment Operator Overloading Why are Postfix ++/— categorized as primary Operators in

  • 0

Possible Duplicate:
Post-increment Operator Overloading
Why are Postfix ++/— categorized as primary Operators in C#?

I saw that I can overload the ++ and -- operators.
Usually you use these operators by 2 ways. Pre and post increment/deccrement an int
Example:

int b = 2; 
//if i write this
Console.WriteLine(++b); //it outputs 3
//or if i write this
Console.WriteLine(b++); //outpusts 2

But the situation is a bit different when it comes to operator overloading:

    class Fly
    {
        private string Status { get; set; }

        public Fly()
        {
            Status = "landed";
        }

        public override string ToString()
        {
            return "This fly is " + Status;
        }

        public static Fly operator ++(Fly fly)
        {
            fly.Status = "flying";
            return fly;
        }
    }


    static void Main(string[] args)
    {
        Fly foo = new Fly();

        Console.WriteLine(foo++); //outputs flying and should be landed
        //why do these 2 output the same?
        Console.WriteLine(++foo); //outputs flying
    }

My question is why do these two last lines output the same thing? And more specifically why does the first line(of the two) output flying?


Solutions is to change the operator overload to:

        public static Fly operator ++(Fly fly)
        {
            Fly result = new Fly {Status = "flying"};
            return result;
        }
  • 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-06-14T18:00:05+00:00Added an answer on June 14, 2026 at 6:00 pm

    The difference between prefix and postfix ++ is that the value of foo++ is the value of foo before calling the ++ operator, whereas ++foo is the value that the ++ operator returned. In your example these two values are the same since the ++ operator returns the original fly reference. If instead it returned a new “flying” Fly then you would see the difference you expect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: post increment operator java What is x after x = x++? Can
Possible Duplicate: Mixing C pre/post increment/decrement with Objective-C dot operator works? I'm debugging a
Possible Duplicate: Pre & post increment operator behavior in C, C++, Java, & C#
Possible Duplicate: In what order does evaluation of post-increment operator happen? Consider the following
Possible Duplicate: Post Increment and Pre Increment concept? I cant understand how the if
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Is it allowed to name the parameter in postfix operator ++? I
Possible Duplicate: Post on a Facebook wall as Page, not as user Can facebook
Possible Duplicate: Can we post image on twitter using twitter API in Android? I
Possible Duplicate: android steganography Has anyone implemented steganography on android? Can anyone post sample

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.