Make a positive number a negative number in 1 line of C# code – A code quiz!
Here are 4 methods, each one is meant to turn a positive number into a negative.
public int ReturnMinusVersion1(int i) { return -i; } public int ReturnMinusVersion2(int i) { return --i -i +i; } public int ReturnMinusVersion3(int i) { return ~i + 1; } public int ReturnMinusVersion4(int i) { return i * -1; }
The questions are:
Which methods compile?
Which methods produce the correct result?
Explain why the working methods work.
Which is best and why?
Any other cool ways of making a positive number a negative? Have fun!
"