It turned out to be like this: how does the auto-increment and auto-subtraction operators overload C #?

Source: Internet
Author: User

In C #, the syntax for reloading the auto-increment and auto-Subtract operators is nothing special, as shown below:

Public static sometype operator ++ (sometype some)
{
// Specific implementation
}

For auto-increment and auto-subtraction operators in C #, no matter the prefix or suffix, only one implementation is required. That is to say, whether I am like this: sometype ++ or like this: ++ sometype uses sometype-Type Auto-incrementing overload, the implementation in the above Code is enough to complete the task. However, prefix ++ and suffix ++ are different. Why do they only need the same implementation to achieve what we need?

In addition, the first principle of the overload operator is to return a new object instead of changing the operand object. Otherwise, it may not only confuse customers who use our overload operators, but also lead to unexpected situations during code debugging. So do we also need to follow this principle for auto-increment and auto-subtraction operators? How can we increase or decrease the number of operations without modifying the number of operations? Consider the following implementation:

class SomeType   
{
public int Number { get; set; }
public static SomeType operator ++(SomeType s)
{
s.Number++;
return s;
}
}

Here, the operand is directly modified and the modified operand instance is returned.

When we use the sometype prefix auto-increment and reload:

SomeType instance = new SomeType();
instance.Number = 1;
++instance;

As we expected, operator overload methods were executed. In addition, the instance will be automatically increased according to the ideal method. Let's take a look at the suffix auto-increment operation:

SomeType instance1 = new SomeType();
instance1.Number = 1;
SomeType instance2 = instance1++;

The less rigorous thinking makes it easy to think that the number of instance1 should be 2, and the number of instance2 should be 1. However, it is not as desirable. In fact, the number of instance1 and instance2 are both 2!

Why?

In fact, compared to other common overload operators such as + and-, the compiler will perform some additional processing on the overloaded auto-increment and auto-subtraction operators. When we use auto-increment overload, methods such as ++ instance and ++ overload will be executed. However, we did not expect that after the operator overload method is executed, the instance will be automatically assigned a value to the return value of the operator overload method! All of this is scheduled during compilation. That is to say, if sometype is of the reference type, after the ++ instance statement is executed, instatnce points to the object instance returned by the auto-incrementing and overloaded operator method. If sometype is of the value type, the instance will be assigned a value of the value type returned by the overload operator method according to the standard C # Value Type assignment method, that is, value-by-field assignment.

This works well when we use prefix. But when we use the suffix, the problem arises. In the above example, the auto-increment operation of instance1 is executed first, but next, instance1 is actually used to assign values to instance2 using a copy before the auto-increment operation is performed (for the reference type, use the reference copy; For the value type, use the copy of the entire structure. In the implementation of auto-increment overload of sometype, operations are directly modified and the original operations are returned. As a result, instance1 and instance2 now point to instances with the original operands. It is strange that they have the same number.

The auto-increment and overload versions of another sometype are as follows:

public static SomeType operator ++(SomeType s)     
{
var result = new SomeType();
result.Number++;
return result;
}

The implementation of this version follows the principle that "the operands should not be modified in Operator Overloading. If the auto-increment feature of this version is used, in the above-mentioned suffix auto-increment example, it will be as expected: the number of instance1 is 2, and the number of instance1 is 1. I think, in many cases (especially when sometype is a value type), this will be the result you want and the expected result of the consumer of your code.

Well, for the auto-increment and auto-subtraction operators, it may be easier to understand this: for example, the statement "instance2 = instance1 ++ ;", instead of assigning the return value of the auto-increment method to the left value of instance2, the return value of the auto-increment method is assigned to instance1. Note: the return value of the auto-increment overload method is used to assign values to the operands that call this overload method! (If you have a C ++ background, this may not be easy to accept)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.