Casting a negative literal value to nint or nuint requires parentheses
See original GitHub issueTake the following sample code:
public class C {
uint x = unchecked((uint)-1); // Ok
uint y = unchecked((uint)(-1)); // Ok - Suggests removing unnecessary parentheses
nuint z = unchecked((nuint)-1); // CS0119 and CS0075
nuint w = unchecked((nuint)(-1)); // Ok
}
I would expect nuint
(and nint
) to also allow casting without surrounding the negative integer literal with parentheses.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Why isn't the C# compiler able to cast a literal negative ...
To cast a negative value, you must enclose the value in parentheses If you are casting using a keyword that identifies a predefined...
Read more >C# Error CS0075 – To cast a negative value, you must ...
Error CS0075 To cast a negative value, you must enclose the value in parentheses. This is because the expression (EmploymentType) -1 is not...
Read more >Native sized integers - C# 9.0 draft feature specifications
This feature specification describes native sized integers, which are integer types that use the processor's natural integral types.
Read more >C# compiler breaking changes since C# 10
In Visual Studio 17.3, the compiler requires spaces before the first parenthesis, the character offset, and the file name.
Read more >4. Advanced C# - C# 10 in a Nutshell [Book]
A delegate instance literally acts as a delegate for the caller: the caller invokes the delegate, and then the delegate calls the target...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Because parser doesn’t know if
type
is actually a type or expression at parse time, so that is parsed as a binary operator with a parenthesized lhs rather than a cast. I think that’s why CS0075 exists after all, as a hint when the identifier determined to be a type.On the other hand, when a keyword is used, it is parsed as a cast from get-go – there’s no way
uint
could be an expression. Note this is not about primitive types themselves,(Int32)-1
has the same ambiguity and yields an error. It must be a keyword.If the cast operator had a higher precedence than a parenthesized expression (which in turn, defined under “primary expressions”), it could work but that is just not how the grammar is specified. If possible at all, it would have need a lot of lookaheads and does not simply fit into other production rules.
Thanks @alrz for the detailed explanation.
nint
andnuint
are identifiers rather than keywords so the current behavior is expected.Closing as “by design”.