Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C# is there any difference between += and =+?

Tags:

c#

If I go

variable1 =+ variable2
variable1 += variable2

I get the same result for variable1.

So is there any difference?

like image 909
Diskdrive Avatar asked Aug 06 '10 02:08

Diskdrive


People also ask

What does |= mean in C?

The ' |= ' symbol is the bitwise OR assignment operator.

What is '~' in C programming?

In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...

What is an operator in C?

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators − Arithmetic Operators.

What is the use of in C?

In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more.


4 Answers

The difference is that your observation is not correct and variable1 =+ variable2 does not add variable2 to variable1, but rather sets variable1 equal to variable2. The line is really variable1 = +variable2, or simply variable1 = variable2.

Consider this code

int a = 10;
int b = 20;

a =+ b;
a += b;

At the end of this process, a equals 40. It is intialized to 10, b is initialized to 20, a is set equal to b, and then b gets added to a.

like image 161
Anthony Pegram Avatar answered Oct 02 '22 08:10

Anthony Pegram


Yes there is a difference.

int x = 0; 

x += 1; --> x = x + 1; (you are adding 1 to x)

x =+ 1; --> x = +1; (you are assigning x a value)
like image 35
SoftwareGeek Avatar answered Oct 02 '22 08:10

SoftwareGeek


Yes, using a toy example I show them as different.

  • In the case of variable1 =+ variable2 you're effectively computing

    variable1 = 0 + variable2
    

    or simply

    variable1 = variable2
    
  • In the case of variable1 += variable2 you're effectively computing

    variable1 = variable1 + variable2
    
like image 22
Mark Elliot Avatar answered Oct 02 '22 08:10

Mark Elliot


Perhaps it's best to state that there is no =+ operator in C#. But you can use a unary + to indicate a positive number (always redundant, but included for completeness).

And for completeness of this answer, x += y is the same as x = x + y.

like image 22
siride Avatar answered Oct 02 '22 10:10

siride