I’m working on an ASP.NET application in Visual Studio 2019. I’m running into a problem I’ve never encountered with C# before. I seemingly cannot declare any integer types other than UInt64.
Trying to declare an Int64 or a long gives me errors, because it says I’m trying to convert a UInt64 (or ulong) into one of those types.
The particular errors are as follows (the Azure tenant ids shown are dummy values, don’t panic):
The line
Int64 tenantInt = 11132432026456784806;
gives the following error
Error CS0266 Cannot implicitly convert type 'ulong' to 'long'. An explicit conversion exists (are you missing a cast?)
and then the following line of code
long TenantID = -11132432026456784806;
gives the following error
Error CS0023 Operator '-' cannot be applied to operand of type 'ulong'
Here is an image of the very same errors:
HERE
I’ve never encountered this when working in C# before. Normally these declarations work fine. Is there something wrong with Visual Studio? It keeps reading the numbers as UInt64, and trying to cast them using (Int64) or (long) is met with another error, that a UInt64 (or ulong) cannot be cast to those types.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You must use the “ulong” because support 18.446.744.073.709.551.615.
Follow the reference: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types
Method 2
Kalten is correct. The numbers I’m trying to assign are too big for Int64 and long. Essentially, the person I got this API from purposefully made the number too big so the line would throw an error so I would see the line and realize I need to replace it with my own ID.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
