-4

I have a piece of code that gives the same output but would like to know why the C# compiler doesn't throw an error with the assignment mentioned in example 2.

Example 1:

int income = 0;       
for (int i = 0; i < 5; i++)
{
    income = income + i;
}

Example 2:

int income = 0;     
for (int i = 0; i < 5; i++)
{
    income = income = income + i;
}
TheSoftwareJedi
  • 34,421
  • 21
  • 109
  • 151
Parthasarathy
  • 19
  • 1
  • 5
  • 8
    It doesn't report an error because it isn't an error. The code is legal in C#, even if not a good idea. – Ben Voigt Aug 27 '18 at 13:17
  • 8
    An assignment returns it's value. https://stackoverflow.com/questions/3807192/why-do-assignment-statements-return-a-value – Tim Schmelter Aug 27 '18 at 13:17
  • Sure. Is there a name for such assignments from MSDN? – Parthasarathy Aug 27 '18 at 13:19
  • Just "assignment". The result of an assignment is the assigned value. So `income = 3` returns `3`. – MakePeaceGreatAgain Aug 27 '18 at 13:21
  • 3
    @Parthasarathy: [_"The assignment operator (=) stores the value of its right-hand operand in the storage location, property, or indexer denoted by its left-hand operand and returns the value as its result"_](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/assignment-operator) – Tim Schmelter Aug 27 '18 at 13:23
  • @Parthasarathy can you let us know what the problem and or issue is with the code? what is it that you are expecting..? also please read up on C# MSDN what Assignment operator is this question you posted is very vague – MethodMan Aug 27 '18 at 13:34
  • Thanks for the reply. I got the answers from the previous comments and understood it is legal to have an assignment such as x = x = x + y. – Parthasarathy Aug 27 '18 at 13:39
  • Nominated for reopening the question as it was perfectly clear, and the answer deserves being awarded. – TheSoftwareJedi Aug 27 '18 at 15:23

1 Answers1

2

From learn.microsoft.com:

The assignment operator (=) stores the value of its right-hand operand in the storage location, property, or indexer denoted by its left-hand operand and returns the value as its result. The operands must be of the same type (or the right-hand operand must be implicitly convertible to the type of the left-hand operand).

The simple assignment operator "=" is used to store the value of its right-hand operand into the memory location denoted by the left-hand operand. The result is its return value.

So, your operation will do: income= (income + i) then (income)=(income), perfetly valid.

If you have problems with understanding income= (income + i), well, operator "+" has a return type, so see it like this int j= (int j+int i) where (int j+int i) has a return type int

This also works on primitive types, if you will try to do it with a user defined class you will have to overload the operator "+"

See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/ for info about operators in c#

victor dabija
  • 505
  • 4
  • 11