C# What's the "+=" for? [duplicate] - c#

This question already has answers here:
C# '+=' Operator
(2 answers)
Closed 6 years ago.
I don't really get what the function of "+=" is.. Could someone explain it to me? Like what's the different between "=" and "+="
I've got something like this now:
b +=(a[i]);
Thanks!

b = a assigns a to b
b += a assigns b+a to b
Ex:
int a = 5;
int b = 2;
b = a; //b is now 5
vs.
int a = 5;
int b = 2;
b += a; //b is now 7

b +=(a[i]);
It is a shortcut for this
b = b + a[i];
https://msdn.microsoft.com/en-us/library/sa7629ew.aspx

b = a[i] + b
But you can use it for add event
Button.Click += new System.EventHandler(Button_Click);

Related

Shortcut syntax for doing var = var + n? [duplicate]

This question already has answers here:
How to increase value of an integer higher than 1 in one command?
(2 answers)
Closed 2 years ago.
I'd like to increment my var value of n, using a similar structure of the post-increment solution like var++.
The post-increment is like this:
int var = 0;
var++; // var = var + 1
I'd like to increment var of n, with n = 4 for example. What's the correct syntax ?
int var = 0;
var++4; // var = var + 4 but obviously not working;
what you are looking for is actually a Compound assignment with arithmetic operators
int a = 5;
a += 9;
Console.WriteLine(a); // output: 14
a -= 4;
Console.WriteLine(a); // output: 10
a *= 2;
Console.WriteLine(a); // output: 20
a /= 4;
Console.WriteLine(a); // output: 5
a %= 3;
Console.WriteLine(a); // output: 2

Dividing results in zero for some reason? [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Division returns zero
(8 answers)
Closed 2 years ago.
I was doing a data science pet project, this is a simplified version of the problem:
int b = 102;
int c = 248;
double a = (b / c) * 100;
Console.WriteLine(a); // prints 0
This code prints zero for some reason. Any other alternatives? Why is this happening?
Because "b / c" is zero, you should use:
int b = 102;
int c = 248;
double a = ((double)b / c) * 100;
int k = Convert.ToInt32(a);
Console.WriteLine(k);

Swap two integers without using a third variable for all range of integer values [duplicate]

This question already has answers here:
Swap two variables without using a temporary variable
(29 answers)
Closed 8 years ago.
How to swap 2 integers without using a third variable such that it works for all ranges of integers.
I know that generally we do the following logic.
int a, b;
a = 10;
b = 30;
a = a + b;
b = a - b;
a = a - b;
But this logic will fail if (a + b) gives the value more than the integer range.
Is there any other logic?
I believe you're looking for the XOR swap:
if (a != b) {
a ^= b;
b ^= a;
a ^= b;
}
You can use xor ...
x ^= y;
y ^= x;
x ^= y;
Source with a handy live demo.
int a=10;
int b=20;
a=a^b;
b=a^b;
a=a^b;
Console.WriteLine(a);
Console.WriteLine(b);

How do you swap two integer value without using temp variable? [duplicate]

This question already has answers here:
Swap two variables without using a temporary variable
(29 answers)
Closed 8 years ago.
// Swapping value between two integer without using temp variable.
int a = 5;
int b = 7;
Console.WriteLine("Before Swap.");
Console.WriteLine("value of A is: {0}", a);
Console.WriteLine("value of B is: {0}", b);
Console.ReadLine();
O/P:
Before Swap.
value of A is: 5
value of A is: 7
After Swap.
value of A is: 7
value of A is: 5
So how can you swap two integer value without using temp variable?
Try:
a = a + b;
b = a - b;
a = a - b;
first method
a = a + b;
b = a - b;
a = a - b;
second method
a ^= b;
b ^= a;
a ^= b;
a = a + b;
b = a - b;
a = a - b;
You need to use the XOR operator:
a = a ^ b;
b = a ^ b;
a = a ^ b;
This works because applying two times the XOR with the same value cancel the operation:
a ^ b ^ b => a

Making a double division [duplicate]

This question already has answers here:
What is the best practice to make division return double in C#
(6 answers)
Closed 8 years ago.
I'm trying to make a double divion with this code:
int a = 5;
int b = 5;
double result = (a + b) / 4;
How to get a 2.5 as a result and not 2, please?
Thanks in advance!
One of the arguments needs to be a double. So:
double result = (a + b) / 4.0;
Or
double result = (double)(a + b) / 4;

Categories

Resources