Search code examples
c#operators

What do the +=, -=, *=, and /= operators mean?


I have been looking everywhere to figure out what these mean and how they are used +=, -=, *=, /=, the most I have found is that they are, "Assignment by Addition", "Assignment by Difference", "Assignment by Product", "Assignment by Quotient", etc, but I can't figure out when or how they are used. If anyone can please explain this to me I would be very grateful. thanks


Solution

  • They are shorthand:

    a += b

    is the same as

    a = a + (b)

    Etc...

    so

    • a -= b is equivalent to a = a - (b)
    • a *= b is equivalent to a = a * (b)
    • a /= b is equivalent to a = a / (b)

    For the reason why there's a parenthesis over the b's, a *= 10 / 5 is not equivalent to a = a * 10 / 5, but rather equivalent to a = a * (10 / 5)

    As Kevin Brydon suggested - Familiarize yourself with the operators in C# here.