The problem is precedence of operators having their table seen in the documentation. The numerical negation operator has a lower precedence than the member access operator.
Applying precedence the code can be read like this:
var textArray1 = new string[] { -(899253461.ToString()) };
When you really wanted it:
var textArray1 = new string[] { (-899253461).ToString() };
So if writing like this works.
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
This makes more sense if you use a variable:
var textArray1 = new string[] { (-numero).ToString() };
If you are actually using a literal it makes no sense to use this form of syntax, you can use a *direct string:
var textArray1 = new string[] { "-899253461" };
It’s also unlikely that it makes sense to have a array Then he only has one element and he can’t be expanded, so why do this? If it were only to demonstrate the case it would be ok, but it is clear from the comments that the code is used in larger context. Of course there is some case where it makes sense and be passed as an argument of a method that expects a array, but in this case probably should only create the value for the argument and not a variable.
Note that this is not a problem of Visual Studio or the lost compiler, it is a well established rule by the language and enforced by the compiler. The programmer should know this rule and apply the code necessary to better show his intention, which in this case is to apply the parentheses to the sign to stay next to the number before to make the conversion to string
. The way you wrote the normal number is converted to string
and then try to apply the negative in this text, so it makes the mistake. And so it is not necessary to separate the number in another line, only put parentheses.
And C# does not use the concepts of primitive types.
{ "-899253461".ToString() };
– CypherPotato
@Cypherpotato If surrounded with quotation marks you do not need to use the method
ToString
.– Jéf Bueno