total += nValue;
so would that mean that whatever the total is it will be added towards nValue, or is something being incremented?
Thanks
total += nValue;
so would that mean that whatever the total is it will be added towards nValue, or is something being incremented?
Thanks
asked whether the += form means "add the left side to the right" — captured the intent: it updates the left-hand target by adding the right-hand value. 's note about other compound forms is useful, but a few extra details are often missed and can change behavior in real code.
The language makes E1 op= E2 convenient but not identical in all respects to writing the longer form. In C# the left-hand operand is evaluated only once (important for properties, indexers or expressions with side effects). The operation uses any applicable user-defined + overload, and the result is implicitly converted back to the left-hand type when needed. See the official notes on compound assignment for precise rules: .
A small example shows the single-evaluation effect:
class Foo {
private int _v;
public int Value {
get { Console.WriteLine("get"); return _v; }
set { Console.WriteLine("set"); _v = value; }
}
}
var f = new Foo();
f.Value += 2; This will call the getter once and the setter once.
Additional cautions: += on delegate/event types performs subscription (not numeric addition). Integral arithmetic and conversions obey checked/unchecked contexts (overflow behavior can change) — see checked keyword. Compound assignment is not atomic for shared counters; prefer atomic APIs such as Interlocked.Increment for thread-safe increments. For operator overloading details, consult operator overloading.
Jump to Post— catherine sea 15It means
total = total + nValue;
It means
total = total + nValue; Also you can have
total = total * nValue // total *= nValue
total = total / nValue // total /= nValue
total = total - nValue // toatl -= nValue
Some time you can Also have something like thing
total *= 0.05M; // total = total * 0.05, Note the present of M. We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.