-1

I was browsing around stack overflow and I encountered this question:

check for duplicate filename when copying files in C#

In this question, this little gem existed:

int i = +1

I have never seen this syntax before. So I opened up the interactive C# window in visual studio:

Microsoft (R) Roslyn C# Compiler version 1.3.4.60902
Loading context from 'CSharpInteractive.rsp'.
Type "#help" for more information.

> int i = +1;
> i
1
> +1 == 1
true

Is this similar to +=? Is this some new syntax? What is this operator? How is it different than a normal variable declaration?

Dudemanword
  • 710
  • 1
  • 6
  • 20
  • An interesting note, in the example question you linked, they used this incorrectly. They were trying to increment an integer variable (as seen in their comment above the line that used `+1`). – Lews Therin Aug 07 '18 at 16:02
  • @LewsTherin: Not so much _"An interesting note"_ - it was the cause of the problem in the linked question - though the accepted answer did not make that totally clear. – PaulF Aug 07 '18 at 16:06

3 Answers3

10

That's the unary plus operator. From the documentation:

The result of a unary + operation on a numeric type is just the value of the operand.

In most sane contexts1 where you're writing code, it'll be optional (+1 is the same as 1 if we're writing literals).

It mostly exists for symmetry with the unary minus operator.

Most of the time, you'll not write code containing it, but if you're generating code it can be handy to be able to apply a unary operator either way2.

It has no relation to +=.


1Insane code could override this operator for custom types and make it more than a no-op. But I'd love to understand a use case where it makes code more understandable, which should be the main aim of most code.

2E.g. imagine you're chaining a set of operations together and for each additional element, you wish to change the sign of the overall result. This lets you just store an operator and apply it blindly when you finally decide to output a result

TheSoftwareJedi
  • 34,421
  • 21
  • 109
  • 151
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • 1
    It could also be useful to say this without the jargon: "It is an optional positive sign. The opposite of a negative sign." – Lews Therin Aug 07 '18 at 15:52
  • I'm not arguing with you, you aren't incorrect. All I was saying is that this website is viewed by people of all levels of experience. "unary plus operator" doesn't mean much to a beginner or even most intermediate developers. I was just trying to simplify your explanation for those who don't speak fluent developerese yet. – Lews Therin Aug 07 '18 at 15:58
  • 1
    @LewsTherin - I've tried to add a bit of exposition into the answer, that may allow more readers to make use of the answer? I still want to make sure the answer addresses the question as written, but let me know if it can be improved. – Damien_The_Unbeliever Aug 07 '18 at 16:09
  • @LewsTherin: I think it is important to understand that the '+' is not an optional sign but is actually an operator as it can be applied to variables as well as constants - in the following the + & - are acting as operators _"int a = 1; Console.WriteLine( +a ); Console.WriteLine( -a );"_ – PaulF Aug 07 '18 at 16:13
  • @Damien_The_Unbeliever Thanks, I think your edits made it much easier to understand. – Lews Therin Aug 07 '18 at 16:28
  • @PaulF Fair, my comment was an oversimplification. But that doesn't detract from my point that the original answer was much harder to understand (for those with less development experience) than in its current form. – Lews Therin Aug 07 '18 at 16:30
  • @LewsTherin: I admit that I really couldn't understand why OP didn't recognise a positive signed integer & needed to ask the question - if it was a + applied to a variable (as in my example above) on the otherhand then I could see it may be seen as a weird new syntax. – PaulF Aug 07 '18 at 20:17
  • Sometimes you must store the sign itself, which can be done like this: `int sign = x > 0 ? +1 : x < 0 ? -1 : 0;`. Here, the + sign underlines our intention and seems to make sense. – Olivier Jacot-Descombes Aug 08 '18 at 13:27
3

For for all signed numeric types the positive-sign is optional. So,

+1 == (+1) == 1
+1.0 == (+1.0) == 1.0
+1L == (+1L) == 1L
+1.0m == (+1.0m) == 1.0m

Do not confuse

int i = +1; // Assigns 1

which is the same as

int i = (+1); // Assigns 1

or simply

int i = 1; // Assigns 1

with

int i += 1; // INCREMENT!

which increments i.


In C# terms there is a binary + operator (the addition operator as in int i = 3 + 4;) and a unary + operator (the plus sign as in int i = +1;).

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
2

Think of it the way you think of

int i = -1

and it becomes obvious

Mel Gerats
  • 2,234
  • 1
  • 16
  • 33