I saw code that goes like this:
double d = GetDouble();
DoSomething(+d);
DoSomething(-d);
I know in it is potentially dangerous and not recommended in C++ to use the unary + just to emphasize that the value is positive. <EDIT>"just to emphasize that the value is positive" was a mental shortcut. I know it doesn't make a negative value positive.</EDIT>
The C# language reference doesn't say much about it:
The unary + operator returns the value of its operand.
There is a question on SO about this, but it is tagged with C, C++ and C#, and none of the answers clearly mentions C#.
As this answer of the question you linked says, unary
+in C(++) does do something, and is not necessarily a no-op. This is true in C# too.C# only has these unary
+operators (See spec):So if
xis ashort,+xwould be of typeint, because the first operator is selected by overload resolution. As a result, something like this does not compile:This also affects overload resolution, among other things. Just like the code presented in this answer, if you do:
C.Foo(x)wherexis ashortwill printshort, butC.Foo(+x)will printint.Does this situations like the above happens often enough that makes
+xa "bad" or "unsafe" practice? That is for you to decide.Of course, if
xis of a custom struct/class type, then+xcould do basically anything. Unary+is overloadable.