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 8186263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:08:06+00:00 2026-06-07T02:08:06+00:00

Is there a special method that you can add if you get an i++

  • 0

Is there a special method that you can add if you get an i++ type of operation on a property?

Here is an example of what I’m trying to do. I know this don’t work, but this gives you an idea of what I’m talking about. Actually, I’m working with two internals and I want to increase one on + and the other on -.

int mynum;
int yournum 
{
    get{ return mynum; }
    set{ mynum = value; }
    set++{ mynum = mynum + 5; return mynum; } //this is what I want to do
}
// elsewhere in the program
yournum++; //increases by 5
  • 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-07T02:08:09+00:00Added an answer on June 7, 2026 at 2:08 am

    It sounds like you want to override the behavior which occurs when ++ is invoked on the property yournum. If so that’s not possible in C# for exactly the code you outlined in your sample. The ++ operator should be incrementing by 1 and every user calling yournum++ would expect that behavior. To change it silently to 5 would certainly lead to user confusion

    It would be possible to get similar behavior by defining a type with a custom ++ operator which did the + 5 conversion instead of +1. For example

    public struct StrangeInt
    {
        int m_value;
    
        public StrangeInt(int value)
        {
            m_value = value;
        }
    
        public static implicit operator StrangeInt(int i)
        {
            return new StrangeInt(i);
        }
    
        public static implicit operator int(StrangeInt si)
        {
            return si.m_value;
        }
    
        public static StrangeInt operator++(StrangeInt si)
        {
            return si.m_value + 5;
        }
    }
    

    If you now defined yourname to be StrangeInt then you would get the behavior you were looking for

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

Sidebar

Related Questions

Is there a special method to get iPhones orientation? I don't need it in
I know that there are certain special methods of various objects that represent operations
is there anything special I need to do to get ASP.NET MVC3 enabled on
Can anyone please tell me is there any special requirement to use either EXTERN
str_replace('Ê','',$line); Ain't working. Is there some special string that represents that?
Say I get a model instance like this: instance = session.query(MyModel).filter_by(id=1).first() How can I
I'm trying to create a custom validation method in JQuery and I can't manage
Here's a tip that makes some confusions for me:to have the best passing method
Is there any way in NHibernate that I can use the following Entities public
Are there special restrictions for naming an event handler under GWT UIBinder? UI template:

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.